Sushantnayak's Techincal Weblog

Technology that matters…

Connect MS-Access using jdbc-odbc in Windows 7 64-bit

Posted by sushantnayak on October 25, 2010

Connect MS-Access using jdbc-odbc in Windows 7 64-bit

I faced this problem while connecting to MS-Access using JDBC-ODBC driver, on my laptop and found out this solution.

Bye-the-way I received the following error initially(in my output console).

Exception: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

My solution as follows…

Step 1

Run the 32-bit odbc driver using

WinKey+R, then copy-paste the below command

C:\Windows\SysWOW64\odbcad32.exe
Step 2

Make a dsn named “AccessDB” or whatever name you want to.

Step 3

Create a new project in eclipse.

Step 4

Change the jre to the java installed inside

C:\Program Files (x86)\java

Use this as “JRE System Library”

Step 5

Use the below code to connect to connect mdb file named “library.mdb”(choose ur mdb file), having the path as

“D:\Study\library.mdb”

import java.sql.Connection;
import java.sql.DatabaseMetaData;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection con = null;
	    try {

	// Setting up the DataSource object
	      sun.jdbc.odbc.ee.DataSource ds
	        = new sun.jdbc.odbc.ee.DataSource();
	      ds.setDatabaseName("AccessDB");
	      ds.setDataSourceName("D:\\Study\\library.mdb");
	// Getting a connection object
	      con = ds.getConnection();

	// Getting database info
	      DatabaseMetaData meta = con.getMetaData();
	      System.out.println("Server name: "
	        + meta.getDatabaseProductName());
	      System.out.println("Server version: "
	        + meta.getDatabaseProductVersion());

	// Closing the connection
	      con.close();
	    } catch (Exception e) {
	      System.err.println("Exception: "+e.getMessage());
	    }
	  }
}

Viola…
Output
Server name: ACCESS
Server version: 04.00.0000

(for any clarification, do ask in my blog, I’ll be happy to explain)…

peace out…

Advertisement

10 Responses to “Connect MS-Access using jdbc-odbc in Windows 7 64-bit”

  1. karthikeyan ellappan said

    hai,,,,
    i am having windows 7 32 bit os…..in my os C:\Windows\SysWOW64\odbcad32.exe path is not available…..
    then how i connect access with Driver….please help me….thanks advance…..

    • csccxczxxzxxxxx said

      just type odbcad32.exe in run box-aarya

  2. vaibhav said

    thanks very much for the solution!!!!
    worked effectively on my system.

    • sajid said

      yaar kaise 3 , 4 step pura kiya jara mail karo please bahut paresan hu is connection se
      3 step kaise kiya
      4 step kaise kiya

  3. Gobruen said

    Connecting to MS Access on Windows 64-Bit

    Folks, like many of you I struggled with the deprecation of JET 4.0 in Windows Vista/7 on 64-bit machines. A process running simply for years, ported to a faster machine simply stopped working with the error: ‘Microsoft.Jet.OLEDB.4.0′ provider is not registered. I was annoyed at the lack of notice from MS and lack of clear instructions. After tinkering I found the solution and I’ll post it first directly, with details after, for the benefit of those just as frustrated as I. The situation I describe is for VB, but may work in other code/environments.

    1. Download and run AccessDatabaseEngine_x64.exe

    (http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c06b8369-60dd-4b64-a44b-84b371ede16d&displaylang=en)

    2. Change the connection string in your code to:

    Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ= <-db name and path here

    There is no need to upgrade/replace JET or emulate 32-bit connections. You don’t have to buy a new version of MS Office/Access, Access does not even need to be installed on the machine, you just need the driver. No need to convert to SQL Express. My legacy code is now running flawlessly on Windows 7 quad 64-bit machine without any office apps installed.

    When I first encountered the issue I tried to replace/upgrade JET but it was not available for 64-Bit and there were no plans to create it. Apparently, there is now a version released for 64-bit windows but you don’t need it. The MS Access Driver exists on older platforms as well and can be used instead of JET on 32-bit machines as well. However, there is one important detail concerning “*.accbd”, you must include this in the string or it wont work. Many examples posted on the web look like this: Driver={Microsoft Access Driver (*.mdb)} But it will produce an error in some cases indicating the DB and driver were not supplied in the string. *.accdb needs to be included as well.

    Replace: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=

    With: Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=

    Full pseudo code:

    dbLocation = "C:\dbstore\myAccess.mdb"
    Set objADO = CreateObject("ADODB.Connection")
    objADO.Open "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & dbLocation

    All other SQL calls and objects are unchanged.

    Have not tried yet in C++, or with Excel, or as a DNS location, but test it yourself.

    • Jan said

      Hi.

      I know that you didn’t test your method with Java (and consequently didn’t vouch for it working with Java), but it would appear that it should work and yet I got an SQLException for the connection when I attempted it. I was wondering whether you or anybody else reading this could shed any light on the problem. Shown below is the Java code that I ran after running AccessDatabaseEngine_x64.exe.

      import java.sql.*;

      public class JDBCTest2
      {
      public static void main(String[] args)
      {
      Connection connection = null;
      Statement statement = null;
      ResultSet results = null;
      String fileName = “F:\\Temp\\Finances.mdb”;
      /*
      I also tried the following (with different fields below):
      String fileName = “F:\\Temp\\ExamResults.accdb”;
      */
      String dbString =
      “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=”
      + fileName + “;DriverID=22;READONLY=false}”;
      //I also tried the above without ‘;DriverID=22;READONLY=false’, but the
      //result was exactly the same.

      try
      {
      Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
      connection = DriverManager.getConnection(dbString, “”, “”);
      }
      catch (ClassNotFoundException cnfEx)
      {
      System.out.println(“* Unable to load driver! *”);
      System.exit(1);
      }
      catch (SQLException sqlEx)
      {
      System.out.println(“* Cannot connect to database! *”);
      //The above message was displayed.
      System.exit(1);
      }

      try
      {
      statement = connection.createStatement();
      results = statement.executeQuery(“SELECT * FROM Accounts”);
      }
      catch (SQLException sqlEx)
      {
      System.out.println(“* Cannot execute query! *”);
      System.exit(1);
      }

      try
      {
      while(results.next())
      {
      System.out.println(“Account no. ” + results.getInt(1));
      System.out.println(“Account holder: ” + results.getString(3)
      + ” ” + results.getString(2));
      System.out.printf(“Balance: %.2f %n%n”,
      results.getFloat(4));
      }

      }
      catch (SQLException sqlEx)
      {
      System.out.println(“* Error retrieving data! *”);
      System.exit(1);
      }
      }
      }

      Any help with this problem would be much appreciated.

      Thanks.
      Jan

  4. Krishanu said

    Thank You very much…its working very good

  5. Neha said

    can u please tell me how to Change the jre to the java installed inside

    C:\Program Files (x86)\java

    using netbeans ?

  6. hi am using windows 7 64 bit,when i connect jdbc the administrative tools contains only sql driver not for ms access driver…how can i install that and how can i create dsn….pls help me….send the download link for ms access driver….please

  7. middleman said

    middleman…

    [...]Connect MS-Access using jdbc-odbc in Windows 7 64-bit « Sushantnayak’s Techincal Weblog[...]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.