Microsoft Access and Java JDBC
Trying to insert some values in a Microsoft access database using java.
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
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.
Microsoft Office 32bit.
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
Ive installed 64 bit version and now im gettin an error [Microsoft][ODBC Driver Manager] Not a valid file name.
At first make sure you can access that database via ODBC. Make DSN in odbcad32
for 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 odbcad32
then make sure it works in 32 bit version of odbcad32
and make sure you use 32 bit version of Java.
Also have a look at other responses to: 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.djcxy.com/p/64908.html