Saturday, April 16, 2011

Connecting Java and MS Access

To build a connection Java and the database required DriverManager. There are two important steps that need your attention. First, initialize the JDBC-ODBC driver via Class.forName(). And second, make objects in accordance with the ODBC connection used, using method DriverManager.getConnection():

Below are examples of program code:



import java.sql.*;

public class AccessCon{
public static void main(String args[]){
Connection con=null;
try{
//initialize the JDBC-ODBC Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
//create a connection object
//username=admin , password=admin
con=DriverManager.getConnection("jdbc:odbc:AccessDB;uid='admin;pw='admin'");

if(!con.isClosed())
System.out.println("Connection Successfully.....");

}catch(Exception e){
System.out.println("Eception: " + e.getMessage());
}finally{
//close connection
try{
if(con!=null)
con.close();
}catch(SQLException e){}
}
}
}


save with filename : AccessCon.java

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates