尝试使用java在Microsoft Access数据库中插入一些值.
我可以犯一个错误,
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
使用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 64位
我安装了64位版本,现在我得到一个错误[微软] [ODBC驱动程序管理器]不是一个有效的文件名.
解决方法:
首先确保您可以通过ODBC访问该数据库.在odbcad32中为64位和32位系统制作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/