Reader Level:
ARTICLE

JDBC Connection to MS-Access in Windows 7

Posted by Abhishek Dubey Articles | Java February 16, 2012
In this article we explain how a Java program can connect to a Microsoft Access Database.
  • 0
  • 0
  • 42180

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

i0.jpg

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

i1.jpg

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

i2.jpg

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.

dsn1.jpg

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

dsn2.jpg

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
dsn3.jpg

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

 Step 9 : Java program code

import java.sql.*;
 
public class MsAcessODBC
     {
   public static void main(String[] args)
            {
            try
              {
              // loading thejdbc odbc driver
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              // 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 (rs.next())
                  {
                  for (int i = 1; i <= numberOfColumns; i++)
                       {
                           if (i > 1)
                          System.out.print(", ");
                          String columnValue = rs.getString(i);
                          System.out.print(columnValue);
                       }
                   System.out.println("");
                   }
            st.close();
            con.close();
               } catch (Exception ex)
                         {
                      System.err.print("Exception: ");
                      System.err.println(ex.getMessage());
                         }
       }
}


OUTPUT :

 cmd.jpg

 Resource

Inserting & Retrieving records from MS Access 2007 using ODBC

COMMENT USING

Trending up