问题
I am trying to connect to MS-Access using JDBC:ODBC:
public boolean connectToAccess(String accessFilePath) {
//Get connection to database
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
myConnection = DriverManager.getConnection("jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
} catch (Exception ex) {
System.out.println(ex);
return false;
}
return true;
}
I get the error: "No suitable driver found for jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=file.mdb" Why? Can you suggest another way of reading access files in Java?
回答1:
Take those spaces out of the connection string and see if that helps. I'd also recommend printing the stack trace.
public boolean connectToAccess(String accessFilePath) {
//Get connection to database
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
myConnection = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
回答2:
The other way to read Access files is using the Jackcess library.
回答3:
Try to create a DSN for the Access database from odbcad32. Another issue may be, the driver is not installed on your machine or you have insufficient privileges.
来源:https://stackoverflow.com/questions/6251308/java-connect-to-ms-access-database-mdb-or-mde