Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Use Microsoft Access with Java

How do you connect to an Access-based database with JDBC?

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Q Two questions:

  1. How do you connect to a Microsoft Access-based database (in a Windows system) using JDBC?
  2. In Class.forName(....) and DriverManager.getConnection(....), what driver and URL should you use?


AThere are two techniques available for connecting to a data source using JDBC. First, if your program will talk to an ODBC data source, you can use the JDBC-ODBC bridge included with your JDK. In the case of Sun, you would use sun.jdbc.odbc.JbdcOdbcDriver.

Specifically, using Class.forName:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


or the command line:

java -Djdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver <program name>


Second, you can connect to your data source through a third-party, vendor-specific bridge. In the case of a third party driver, you simply substitute that driver for the JDBC-ODBC bridge driver.

See the Resources section below for a link to Sun's database of available drivers.

Once you have a driver loaded and your data source set up, getting a connection is a simple matter of feeding the correct URL, name, and password to the getConnection() method.

A JDBC URL takes the following form:

jdbc:<subprotocol>:<subname>


In the case of Microsoft Access and the ODBC bridge, it would take the form:

jdbc:odbc:<NAME>


where <NAME> is the name that you gave the data source when you set it up.

As an aside for those using other drivers, <subprotocol> is the name of the connectivity mechanism used by the driver.

In my answer, I've assumed quite a bit of JDBC knowledge. If you're completely new to JDBC, or even if you just need to brush up your skills a bit, head on over to the JDBC Short Course (see Resources), an excellent training course that will get you up to speed quickly.

About the author

Tony Sintes is a principal consultant at BroadVision. Tony, a Sun-certified Java 1.1 programmer and Java 2 developer, has worked with Java since 1997.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (3)
Login
Forgot your account info?

jar fileBy Anonymous on February 1, 2010, 2:57 amhow to download jdbc ms access jar file

Reply | Read entire comment

second thatBy Anonymous on September 17, 2009, 4:05 pmI second this question. I am guessing since an image is simply a binary file, I would be trying to retrieve binary data but I am still unsure.

Reply | Read entire comment

How to store/retrieve image from MS Access using Java ?By Anonymous on February 7, 2009, 1:15 amCan anyone help please ?

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.