在 Java 中为 MS Access 创建无 DSN 连接

发布于 2024-10-17 19:23:31 字数 769 浏览 10 评论 0原文

我正在构建一个需要与 MS Access 数据库通信的桌面应用程序。现在,除非我想在每台将使用桌面应用程序的计算机上注册数据库的 DSN,否则我需要一种以无 DSN 的方式连接到数据库的方法。

我进行了大量搜索并找到了一些关于如何创建连接字符串的有用链接基于此,我尝试在此基础上修改我的程序,但没有成功。 下面的代码失败了。如果我将 getConnection 中的字符串切换为“jdbc:odbc:sampleDB”,它可以工作,但这是使用 DSN,而不是我想要实现的目标。

如何在java中编写和使用连接字符串来建立与MS Access数据库的无DSN连接?

private Connection setupConnection() throws ClassNotFoundException,
        SQLException {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("Driver={Microsoft Access Driver (*.mdb)} &_ Dbq=c:\\as\\sampleDB.mdb");
    return con;
}

补充:我还想指出,如果有人知道一种方法为了通过 DSN 连接实现我的要求,我很乐意倾听!

I'm building a desktop app that needs to communicate with a MS Access database. Now, unless I want to register the DSN for the database on every computer that's going to use the desktop app, I need a way to connect to the database in a DSN-less fashion.

I've searched alot and found some useful links on how to create connection strings and based on that I tried modifying my program based on that but without success.
The code below fails. If i switch the string in the getConnection to "jdbc:odbc:sampleDB" it works, but that's using DSN and not what I want to achieve.

How do I write and use a connection string in java to make a DSN-less connection to a MS Access database?

private Connection setupConnection() throws ClassNotFoundException,
        SQLException {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("Driver={Microsoft Access Driver (*.mdb)} &_ Dbq=c:\\as\\sampleDB.mdb");
    return con;
}

Addition: I'd also like to point out that if anyone has an idea of a way to achieve what I asked for WITH a DSN-connection I'll gladly listen to it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜清冷一曲。 2024-10-24 19:23:31

JDBC 连接字符串应以 jdbc: 开头,例如:

jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\Nwind.mdb

所以尝试使用:

   Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\\as\\sampleDB.mdb");

如果配置 DSN,则可以使用更简单的连接字符串连接到它:jdbc:odbc:[alias] , 例子:

jdbc:odbc:northwind

JDBC connection string shouls start with jdbc: like:

jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\Nwind.mdb

so try with:

   Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\\as\\sampleDB.mdb");

If you configure DSN then you can connect to it using simplier connect string: jdbc:odbc:[alias], example:

jdbc:odbc:northwind
耶耶耶 2024-10-24 19:23:31

我也遇到了这个问题,并尝试了这里和各个论坛上的许多建议。最后,我发现了一个地方的一个片段,它导致了成功的连接,并且还解释了为什么其中许多帖子不起作用。请参阅 http://www.coderanch.com/ t/295299/JDBC/databases/jdbc-odbc-DSN-connection-MS

问题是 odbc 末尾的冒号后面必须有一个分号,如 jdbc:odbc:;Driver= 。在阅读关于 JdbcOdbc 桥的 Oracle 文档后,这是有道理的,该文档指出语法为 jdbc:odbc:dsn;属性......由于我们不提供 DSN,因此我们需要以 ; 结尾在添加属性之前。

下面显示了我在 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 和 stmt3 为空,因为连接为空。 stmt 和 stmt2 工作。 stmt2 使用我在 IBM Tivoli 文档中找到的连接字符串。它之所以有效,是因为“MS Access Database”是 ODBC 注册表中的有效标题,作为我计算机上的用户 DSN。

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

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.

I am showing below the tests I ran with different connection strings on a Windows 7 Ultimate 32bit machine:

        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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文