尝试连接UCanAccess时“找不到合适的驱动程序”错误

时间:2015-06-11 23:30:37

标签: java database ms-access-2007 runtime-error ucanaccess

我有以下代码,我根据这篇文章进行了修改 回答:

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");
        }
    }
}

我在运行它时遇到以下错误...

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)

我还在Eclipse构建路径中包含了UCanAccess的5个JAR文件。

什么可能导致问题?

1 个答案:

答案 0 :(得分:3)

jdbc网址错误,你输了//

JDBC:ucanaccess:// C:\ JavaBooks \ java12目录\ Library.mdb