1

我正在使用此程序连接 Ms Access 2007 并获得异常,请帮帮我。例外是,我检查了许多教程,但没有得到最佳解决方案。

java.sql.SQLException:[Microsoft][ODBC Microsoft Access Driver] 找不到文件“(未知)”。

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 multithreading.dbAccess.main(dbAccess.java:14)

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=payroll.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();
    }
}
}`
4

1 回答 1

2

你可以参考这个。我会帮你的

另外,转到控制面板-> 管理工具-> 数据源-> 添加-> Microsoft Access Drivers(*mdb,*accdb)-> 确定-> 确定-> 确定。它可能适用于 ODBC 连接。

此外,您必须提供 payroll.mdb 的正确路径

DBQ=payroll.mdb

于 2013-04-09T04:29:34.883 回答