Java 访问数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19623100/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:53:41  来源:igfitidea点击:

Java Access DB Connection

javadatabase-connectionjdbc-odbc

提问by user2167382

I try to make project with connection to db (MS Access 2010) I use this tutorial on CodeProject.

我尝试制作与 db 连接的项目(MS Access 2010)我在 CodeProject 上使用本教程。

import java.sql.*;

public class DbAccess
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database = 
              "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDB.mdb;";
            Connection conn = DriverManager.getConnection(database, "", "");
            Statement s = conn.createStatement();

            // create a table
            String tableName = "myTable" + String.valueOf((int)(Math.random() * 1000.0));
            String createTable = "CREATE TABLE " + tableName + 
                                 " (id Integer, name Text(32))";
            s.execute(createTable); 

            // enter value into table
            for(int i=0; i<25; i++)
            {
              String addRow = "INSERT INTO " + tableName + " VALUES ( " + 
                     String.valueOf((int) (Math.random() * 32767)) + ", 'Text Value " + 
                     String.valueOf(Math.random()) + "')";
              s.execute(addRow);
            }

            // Fetch table
            String selTable = "SELECT * FROM " + tableName;
            s.execute(selTable);
            ResultSet rs = s.getResultSet();
            while((rs!=null) && (rs.next()))
            {
                System.out.println(rs.getString(1) + " : " + rs.getString(2));
            }

            // drop the table
            String dropTable = "DROP TABLE " + tableName;
            s.execute(dropTable);

            // close and cleanup
            s.close();
            conn.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

But i get strange Exception : java.sql.SQLException: [Microsoft][????????? ????????? ODBC] ???????? ?????? ?? ?????? ? ?? ?????? ???????, ???????????? ?? ?????????

但我得到奇怪的异常:java.sql.SQLException: [Microsoft][????????? ????????? ODBC] ??????? ???????? ??????? ?? ?????????????, ???????????? ?? ?????????

java.sql.SQLException: [Microsoft][????????? ????????? ODBC] ???????? ?????? ?? ?????? ? ?? ?????? ???????, ???????????? ?? ????????? at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956) at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113) at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072) at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323) at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174) at java.sql.DriverManager.getConnection(DriverManager.java:579) at java.sql.DriverManager.getConnection(DriverManager.java:221) at dbaccess.DbAccess.main(DbAccess.java:28)

java.sql.SQLException: [Microsoft][????????? ????????? ODBC] ??????? ???????? ??????? ?? ?????????????, ???????????? ?? ????????? 在 sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956) 在 sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113) 在 sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect7.java:6956 ) 在 sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323) 在 sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174) 在 java.sql.DriverManager.getConnection(DriverManager.java:579)在 java.sql.DriverManager.getConnection(DriverManager.java:221) 在 dbaccess.DbAccess.main(DbAccess.java:28)

I google it and find other questions on Stack like this : Stack Post

我在谷歌上搜索并在 Stack 上找到了其他问题,如下所示:Stack Post

So i add all ODBC drivers that can help me connect *.mdb file. But nothing good hepend.(

所以我添加了所有可以帮助我连接 *.mdb 文件的 ODBC 驱动程序。但没有什么好结果。(

What is it and how connect to Access DB?

它是什么以及如何连接到 Access DB?

采纳答案by Gord Thompson

There is nothing fundamentally wrong with your code because I pasted it into Eclipse and it ran fine. The only change I made was to specify the path to the database file, i.e., instead of using

您的代码从根本上没有任何问题,因为我将其粘贴到 Eclipse 中并且运行良好。我所做的唯一更改是指定数据库文件的路径,即,而不是使用

DBQ=myDB.mdb

I used

我用了

DBQ=C:\__tmp\myDB.mdb

I was also running it under a 32-bit JVM (on a 32-bit computer). So, my suggestions would be

我也在 32 位 JVM(在 32 位计算机上)下运行它。所以,我的建议是

  1. Try specifying the complete path to the .mdbfile like I did.

  2. If you still get an error, check your Java environment to see if your application is running in a 64-bit JVM. If it is, then Driver={Microsoft Access Driver (*.mdb)}will not work: there is no 64-bit version of the older Jet ODBC driver. In that case you have two options:

    i. Configure your application to run in a 32-bit JVM, or

    ii. Download and install the 64-bit version of the Access Database Engine from here, and then use Driver={Microsoft Access Driver (*.mdb, *.accdb)}.

  1. 尝试.mdb像我一样指定文件的完整路径。

  2. 如果仍然出现错误,请检查您的 Java 环境以查看您的应用程序是否在 64 位 JVM 中运行。如果是,那么Driver={Microsoft Access Driver (*.mdb)}将不起作用:没有 64 位版本的旧 Jet ODBC 驱动程序。在这种情况下,您有两个选择:

    一世。将您的应用程序配置为在 32 位 JVM 中运行,或者

    ii. 从这里下载并安装 64 位版本的 Access 数据库引擎,然后使用Driver={Microsoft Access Driver (*.mdb, *.accdb)}.

回答by Gord Thompson

Now that the JDBC-ODBC Bridge has been removed from Java 8 a better approach would be to use the UCanAccessJDBC driver. For more information see

既然 JDBC-ODBC Bridge 已从 Java 8 中删除,更好的方法是使用UCanAccessJDBC 驱动程序。有关更多信息,请参阅

Manipulating an Access database from Java without ODBC

在没有 ODBC 的情况下从 Java 操作 Access 数据库