Reader Level:

JDBC Connection to MS-Access in Windows 7

By Abhishek Dubey on Feb 16, 2012
In this article we explain how a Java program can connect to a Microsoft Access Database.

JDBC connection to Microsoft Access in Windows 7

Introduction -
In this article we make a connection using JDBC to a Microsoft Access database. This connection is made with the help of a JdbcOdbc driver. You need to use the following steps for making the connection to the data base.

Creating a Database

Step 1 : Open Microsoft Access and select  Blank data base option and give the data base name as File name option


Step 2 : Create a table and insert your data into the table


Step 3 : Save the table with the desired name; in this article we save the following records with the table name student.


Now Creating DSN of your data base

Step 4 : Open your Control Panel and than select Administrative Tools.

Step 5 : Click on Data Source(ODBC)-->System DSN.


Step 6 : Now click on add option for making a new Microsoft Access Driver (*.mdb. *.accdb) and than click on Finish


Step 7 :  Make your desired Data Source Name and then click on the Select option, for example in this article article we us the name mydsn

Step 8 : Now you select your data source file for storing it and then click ok and then click on Create and Finish

 Step 9 : Java program code

import java.sql.*;
public class MsAcessODBC
   public static void main(String[] args)
              // loading thejdbc odbc driver
              // creating connection toth data base
              Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","","");
              Statement st = con.createStatement();
              // create an execute sql command on database   
              ResultSet rs = st.executeQuery("Select * from student order by rollno asc");
              ResultSetMetaData rsmd = rs.getMetaData();
              // this getColumnCount reurn the number of column in the selected table
              int numberOfColumns = rsmd.getColumnCount();
             // while loop and with while loop code use for print the data
             while (
                  for (int i = 1; i <= numberOfColumns; i++)
                           if (i > 1)
                          System.out.print(", ");
                          String columnValue = rs.getString(i);
               } catch (Exception ex)
                      System.err.print("Exception: ");




Inserting & Retrieving records from MS Access 2007 using ODBC