Microsoft Access 和 Java JDBC-ODBC 错误

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

Microsoft Access and Java JDBC-ODBC Error

javajdbcodbc

提问by user1638362

Trying to insert some values in a Microsoft access database using java.

尝试使用 java 在 Microsoft Access 数据库中插入一些值。

I can an error however,

但是,我可以出错,

java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application Exception in thread "main" java.lang.NullPointerException

java.sql.SQLException:[Microsoft][ODBC 驱动程序管理器] 指定的 DSN 包含线程“main”中的驱动程序和应用程序异常之间的体系结构不匹配 java.lang.NullPointerException

To create the data source im using SysWoW64 > odbcad32 and adding it the datasource to system DNS. I say this as i have seen else where there are problems which occur with 64bit systems. However it still doesn't work for me.

要使用 SysWoW64 > odbcad32 创建数据源,并将其添加到系统 DNS 中。我这么说是因为我在其他地方看到的 64 位系统出现问题。但是它仍然对我不起作用。

Microsoft Office 32bit.

微软办公室 32 位。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class AuctionHouseJDBC {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String theItem = "Car";
        String theClient="Name";
        String theMessage="1001";



Connection conn =null; // Create connection object

        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Driver Found");
        } catch(Exception e) {
            System.out.println("Driver Not Found");
            System.err.println(e);
        }

        // connecting to database
        try{
            String database ="jdbc:odbc:Driver={Microsoft Access Driver (*.accdb)};DBQ=AuctionHouseDatabase.accdb;";

            conn = DriverManager.getConnection(database,"","");

            System.out.println("Conn Found");
        }
        catch(SQLException se) {
            System.out.println("Conn Not Found");
            System.err.println(se);
        }
        // Create select statement and execute it

        try{        
            /*String insertSQL = "INSERT INTO AuctionHouse VALUES (  "
                    +"'" +theItem+"', "  
                    +"'" +theClient+"', "
                    +"'" +theMessage+"')";  
            */

            Statement stmt = conn.createStatement();
            String insertSQL = "Insert into AuctionHouse VALUES ('Item','Name','Price')";

             stmt.executeUpdate(insertSQL);
            // Retrieve the results

            conn.close();
        } catch(SQLException se) {
            System.out.println("SqlStatment Not Found");
            System.err.println(se);
        }

    }

}

StaceTrace:

跟踪:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)

Microsoft Office 64bit

微软办公室 64 位

Ive installed 64 bit version and now im gettin an error [Microsoft][ODBC Driver Manager] Not a valid file name.

我已经安装了 64 位版本,现在我得到一个错误 [Microsoft][ODBC 驱动程序管理器] 不是一个有效的文件名。

回答by Micha? Niklas

At first make sure you can access that database via ODBC. Make DSN in odbcad32for both 64 and 32 bit systems. Then as JDBC connect string use: jdbc:odbc:[CreatedDSN]. If you cannot connect to Access in 64 bit version of odbcad32then make sure it works in 32 bit version of odbcad32and make sure you use 32 bit version of Java.

首先确保您可以通过 ODBC 访问该数据库。odbcad32为 64 位和 32 位系统制作 DSN 。然后作为 JDBC 连接字符串使用:jdbc:odbc:[CreatedDSN]. 如果您无法在 64 位版本中连接到 Access,请odbcad32确保它在 32 位版本中工作,odbcad32并确保您使用 32 位版本的 Java。

Also have a look at other responses to: Can't connect to MS Access DB with Windows-64bit

也看看其他回应:Can't connect to MS Access DB with Windows-64bit

Especially interesting is link to: http://www.selikoff.net/2011/07/26/connecting-to-ms-access-file-via-jdbc-in-64-bit-java/

特别有趣的是链接到:http: //www.selikoff.net/2011/07/26/connecting-to-ms-access-file-via-jdbc-in-64-bit-java/