Java:连接到 MS-Access 数据库(mdb 或 mde)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6251308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Connect to MS-Access Database (mdb or mde)
提问by Stefanos Kargas
I am trying to connect to MS-Access using JDBC:ODBC:
我正在尝试使用 JDBC:ODBC 连接到 MS-Access:
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?
我收到错误消息:“找不到适合 jdbc 的驱动程序:odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=file.mdb” 为什么?你能建议另一种在 Java 中读取访问文件的方法吗?
回答by duffymo
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;
}
回答by Sachin
回答by Mukesh Koshy M
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.
尝试从 odbcad32 为 Access 数据库创建 DSN。另一个问题可能是,您的计算机上未安装驱动程序或您的权限不足。