"No suitable driver found" error when trying to connect with UCanAccess

4.6k views Asked by At

I have the following code that I have modified according to this post's answer:

Manipulating an Access database from Java without ODBC

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Main {

    void reportTotalCopies(Connection connection)  
    {
      try{
        Statement statement1 = connection.createStatement();
        Statement statement2 = connection.createStatement();
        String Sql = "SELECT [Κωδικός], [Τίτλος], [Πλήθος Αντιγράφων] FROM [Βιβλίο] ";

        ResultSet set1 = statement1.executeQuery(Sql);
        while (set1.next()) 
        {
            int code = set1.getInt(1);
            String title = set1.getString(2);
            int copies = set1.getInt(3);
            Sql = "SELECT COUNT(Δανεισμός."
                + "[Αριθμός Ταυτότητας Μέλους]) "
                + "AS [Δανεισμένα] "
                + "FROM Δανεισμός "
                + "WHERE Δανεισμός.[Κωδικός Βιβλίου] = " 
                + code
                + " GROUP BY Δανεισμός.[Κωδικός Βιβλίου]";
            ResultSet set2 = statement2.executeQuery(Sql);
            if (set2.next()) 
                copies += set2.getInt(1);
            System.out.printf("%-60s%d\n", title, copies);
        }
      }
      catch(SQLException ex){
          while(ex != null){
              ex.getMessage();
              ex.getNextException();

          }
      }
    }

    public static void main(String args[]) {


        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            //String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + filename + ";DriverID=22;READONLY=true}";
            String database = "jdbc:ucanaccess:\"Library.accdb\"";
            Connection con = DriverManager.getConnection("jdbc:ucanaccess:C:\\JavaBooks\\Java12\\Library.mdb");
            new Main().reportTotalCopies(con);
            //new Java1202().reportBooksByMember(connection);
            con.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Here");
        }
    }
}

and I get the following error from running it...

java.sql.SQLException: No suitable driver found for jdbc:ucanaccess:C:\JavaBooks\Java12\Library.mdb
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Main.main(Main.java:52)

I have also included the 5 JAR files for UCanAccess in the Eclipse build path.

What could cause the problem?

1

There are 1 answers

0
jamadei On

The jdbc url is wrong, you lost a //

jdbc:ucanaccess://C:\JavaBooks\Java12\Library.mdb