java 在 MS ACCESS 中创建 jdbc odbc 连接而不创建 DSN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14157612/
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
creating jdbc odbc connection without creating the DSN in MS ACCESS
提问by Adesh singh
I am trying to make the connection between java and ms access database. I want to make the connection without creating the DSN. I am using the following code but it is throwing the exception "Data source name not found exception "
我正在尝试在 java 和 ms access 数据库之间建立连接。我想在不创建 DSN 的情况下建立连接。我正在使用以下代码,但它抛出异常“未找到数据源名称异常”
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:Driver={Microsoft Access
Driver(*.mdb)}; dbq=d:/newfolder/db11.mdb");
Statement st=con.createStatement();
}
catch(Exception ex)
{
ex.printStackTrace();
}
采纳答案by AsirC
it should be like this:
它应该是这样的:
Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=/db.accdb");
回答by Geoffrey Malafsky
I also had this problem and tried many of the suggestions here and on various forums. Finally, I discovered a snippet from one place which led to success connecting and also explains why many of these posts do not work. See http://www.coderanch.com/t/295299/JDBC/databases/jdbc-odbc-DSN-connection-MS
我也遇到了这个问题,并在这里和各种论坛上尝试了许多建议。最后,我从一个地方发现了一个片段,它导致成功连接,并解释了为什么这些帖子中的许多都不起作用。见http://www.coderanch.com/t/295299/JDBC/databases/jdbc-odbc-DSN-connection-MS
The issue is that there must be a semicolon after the colon at the end of odbc as in jdbc:odbc:;Driver= . This made sense after reading the Oracle documentation on the JdbcOdbc bridge which states that the syntax is jdbc:odbc:dsn; attributes....... Since we are not supplying a DSN, then we need to end with ; before adding attributes.
问题是 odbc 末尾的冒号后面必须有一个分号,如 jdbc:odbc:;Driver= 。在阅读了关于 JdbcOdbc 桥接的 Oracle 文档后,这是有道理的,该文档指出语法是 jdbc:odbc:dsn; 属性.......既然我们不提供DSN,那么我们需要以;结尾。在添加属性之前。
I am showing below the tests I ran with different connection strings on a Windows 7 Ultimate 32bit machine:
我在下面显示了我在 Windows 7 Ultimate 32 位机器上使用不同连接字符串运行的测试:
driver= (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
//jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ= does lookup to ODBC.ini to find matching driver
try {
connstr= "jdbc:odbc:;Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn= DriverManager.getConnection(connstr, "", "");
stmt= conn.createStatement();
}
catch (Exception e){}
try {
connstr= "jdbc:odbc:;Driver={Microsoft Access Driver (*.mdb)};DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn1= DriverManager.getConnection(connstr, "", "");
stmt1= conn1.createStatement();
dbmeta1=conn1.getMetaData();
}
catch (Exception e){}
try {
connstr= "jdbc:odbc:MS Access Database;DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn2= DriverManager.getConnection(connstr, "", "");
stmt2= conn2.createStatement();
dbmeta2=conn2.getMetaData();
}
catch (Exception e){}
try {
connstr= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn3= DriverManager.getConnection(connstr, "", "");
stmt3= conn3.createStatement();
dbmeta3=conn3.getMetaData();
}
catch (Exception e){}
stmt1 and stmt3 are null since the connections are null. stmt and stmt2 work. stmt2 uses a connection string I found in the documentation for IBM Tivoli. It works because "MS Access Database" is a valid title in the ODBC registry as a User DSN on my computer.
stmt1 和 stmt3 为空,因为连接为空。stmt 和 stmt2 工作。stmt2 使用我在 IBM Tivoli 文档中找到的连接字符串。它之所以有效,是因为“MS Access 数据库”是 ODBC 注册表中作为我计算机上的用户 DSN 的有效标题。
回答by Bhavik Shah
JDBC connection string start with jdbc:
like:
JDBC 连接字符串以jdbc:
如下开头:
jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=d:\newfolder\db11.mdb