尝试使用java在Microsoft Access数据库中插入一些值。
但我可以提出错误,
java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]指定的 DSN包含驱动程序和驱动程序之间的体系结构不匹配 线程“main”java.lang.NullPointerException
中的应用程序异常
使用SysWoW64创建数据源> odbcad32并将数据源添加到系统DNS。我说这是因为我已经看到64位系统出现问题。但它仍然不适合我。
Microsoft Office 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位版本,现在我发现错误[Microsoft] [ODBC驱动程序管理器]不是有效的文件名。
答案 0 :(得分:3)
首先确保您可以通过ODBC访问该数据库。对于64位和32位系统,在odbcad32
中生成DSN。然后作为JDBC连接字符串使用:jdbc:odbc:[CreatedDSN]
。如果无法在64位版本的odbcad32
中连接到Access,请确保它在32位版本的odbcad32
中工作,并确保使用32位版本的Java。
另请参阅对Can't connect to MS Access DB with Windows-64bit
的其他回复特别有趣的是链接到:http://www.selikoff.net/2011/07/26/connecting-to-ms-access-file-via-jdbc-in-64-bit-java/