Powered by Blogger.

Connecting MS Access Database using JDBC Driver

>> Sunday, August 4, 2013

Connecting MSAccess Database using JDBC Driver_JavabynataraJTo connect MS Access database using java we should use JDBC drivers.
We should follow below given steps to connect MS Access database using JDBC.
  • Create MS Access database file
  • Create a table in the database
  • Create a Data Source Name using Microsot Access Driver
  • Write a java program to connect MS Access database
Here we are just connecting to MS Access database to test our connection. Apart from that nothing we are doing. We can use MS Access as a database for minor and small scale projects. Adding and retrieving data is so simple with this and several advantages like Easy to Use,Great Storage Capacity, Flexibility, Data Importing and Exporting, Multi User support and Remote Use.


Step #1: Create MS Access database.
Open Microsoft Office Access from the All programs. Open MSAccess then select 'Blank Database',enter the file name as "AccessDB" then click Create button.

Step #2: Create a table in the database File.
Here we are not retrieving data using our program but we should have a Access database to run our program. Create a table named 'Employee' and save it in "F:\JDBC " folder.

Connecting MSAccess Database using JDBC Driver_JavabynataraJ

Step #3:  Create a Data Source Name for MSAccess.
Open Control Panel and select Administrative tools then select Data Sources (ODBC).
Connecting MSAccess Database using JDBC Driver_JavabynataraJ
 Click on Add button to select Microsoft Access Driver
Connecting MSAccess Database using JDBC Driver_JavabynataraJ 
Select *.mdb, *.accdb driver for our datasource
 Connecting MSAccess Database using JDBC Driver_JavabynataraJ 
Enter the Data Source Name(dsn) as 'AccessDSN ', then select Database file 'AccessDB' from F:\JDBC folder. We should select a database to Access driver.
Connecting MSAccess Database using JDBC Driver_JavabynataraJ
then click on ok button.
 Step: #4: Write a java program to connect MS Access database.
Here we are using JDBC Type I driver(sun.jdbc.odbc.JdbcOdbcDriver). Here we given Driver Connection as "jdbc:odbc:AccessDSN" to connect MS Access Database. AccessDSN is the Data Source Name as shown above.
package com.javabynataraj;

import java.sql.Connection;
import java.sql.DriverManager;

//javabynataraj.blogspot.com
 
public class MSAccessConnTest
{
    public static void main(String[] args)
    {
        try
          {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver
      System.out.println("---Driver Loaded---");
           Connection con = DriverManager.getConnection("jdbc:odbc:AccessDSN"); 
    //Create Connection with Data Source Name : AccessDSN
           System.out.println("---Connection Established---"); 
          }
          catch (Exception e)
          {
            System.out.println("Exception : "+e);
          }
    }
}

Output: 
we have a package here com.javabynataraj so you should use the below command while compiling and running the program.
 Successfully we have connected to MSAccess database using JDBC driver.

Reference Books:

Related Posts Plugin for WordPress, Blogger...

About Author

My Photo
GeekDude
Hi, this is Muralidhar from Chennai.I like blogging on some known things. Trying to provide some basic information for java beginners.
View my complete profile

Random Posts

© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.