I'm trying to connect to a database made by MS Access using Java, but I cannot seem to manage. I am using ODBC and I'm getting this exception:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
My Java:
package javaapplication2;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author Owner
*/
public class JavaApplication2 {
/**
* @param args the command line arguments
*
*/
public static void main(String[] args) {
// TODO code application logic here
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL = new String("jdbc:odbc:myDatabase");
System.out.println(sourceURL);
Connection dbConnection = DriverManager.getConnection(sourceURL,"admin","");
Statement myStmt = dbConnection.createStatement();
String query = "INSERT INTO People(ID, Name, Surname, Age, Contact, Location, Course) VALUES"
+ " (1007, 'Elroy', 'Smith', '33', 21366688, 'Somewhere', 'somecourse')";
myStmt.executeUpdate(query);
ResultSet results = myStmt.executeQuery("SELECT * FROM People");
while(results.next())
{
System.out.print(results.getString(1));
System.out.print(results.getString(2));
System.out.print(results.getString(3));
System.out.println(results.getString(4));
}
results.close();
}
catch(ClassNotFoundException cnfe)
{
System.out.println(cnfe);
}
catch(SQLException sqle)
{
System.out.println(sqle);
}
}
}
A little late, but since I've run into the same problem, in your exact scenario, I figured I'd add my solution.
I have Windows 7 (64-bit) and Office 2010 (32-bit). I tried with the DSN-less connection string:
and I tried with the DSN connection, using both the System32 and SysWOW64 versions of the ODBC Admin, and none of that worked.
What finally worked, was to match the bit version of Java with the bit version of Office. Once I did that, I could use either the DSN or DSN less connection mode, without any fuss.
I saw this answer and it worked for me. https://msdn.microsoft.com/en-us/library/ms712362%28v=vs.85%29.aspx
After you have installed an ODBC driver from the driver's setup program, you can define one or more data sources for it. The data source name (DSN) should provide a unique description of the data; for example, Payroll or Accounts Payable. The user and system data sources that are defined for all currently installed drivers are listed in the User DSN or System DSN tabs of the ODBC Data Source Administrator dialog box. The file data sources in a given directory are listed in the File DSN tab; the directory to be shown is entered in the Look in box in the File DSN tab. System_CAPS_noteNote
To manage a data source that connects to a 32-bit driver under 64-bit platform, use c:\windows\sysWOW64\odbcad32.exe. To manage a data source that connects to a 64-bit driver, use c:\windows\system32\odbcad32.exe. In Administrative Tools on a 64-bit Windows 8 operating system, there are icons for both the 32-bit and 64-bit ODBC Data Source Administrator dialog box.
If you use the 64-bit odbcad32.exe to configure or remove a DSN that connects to a 32-bit driver, for example, Driver do Microsoft Access (*.mdb), you will receive the following error message:
The specified DSN contains an architecture mismatch between the Driver and Application
To resolve this error, use the 32-bit odbcad32.exe to configure or remove the DSN.
A data source associates a particular ODBC driver with the data you want to access through that driver. For example, you might create a data source to use the ODBC dBASE driver to access one or more dBASE files found in a specific directory on your hard disk or a network drive. Using the ODBC Data Source Administrator, you can add, modify, and delete data sources, as described in the following table.
Go to this link and download ODBC Driver for 64 bits OS.
http://www.microsoft.com/en-us/download/details.aspx?id=13255
If you are connecting from a 64-bit platform using a 32-bit driver, then run the executable C:\Windows\SysWOW64\odbcad32.exe and create the DSN. It will work.
By default, the Command Prompt is connected to System32. Run a 64-bit command prompt, i.e.,
C:\WINDOWS\SYSWOW64\CMD.EXE
. In that, compile and run your java application.Have you created the DSN first in Control Panel>Administrative Tools>ODBC>System DSN. Name it same as "myDatabase" and if i is asking for locating the database/access file specify the path using browse option. Once ur DSN will be created successfully you will be easily able to access ur DB.