Saturday, February 5, 2011

Connect MS Access database using Type - I JDBC driver

import java.sql.*;

/*********************************************************************************
Program to demonstrate connection to MS Access database using JDBC driver Type - I
  Developed by : Malhar Vora
  Developed on : 6-1-2011
  Development Status : Completed
  Email              : vbmade2000@gmail.com
  WebSite            : http://malhar2010.blogspot.com
**********************************************************************************/

public class dbcon
{
public static void main(String[] args)
{


 Connection conn=null;
 Statement st=null;
 ResultSet rs=null;
 try { 
  Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
 } 
 catch (Exception E) {
  System.err.println ("Unable to load driver.");
  E.printStackTrace ();
 } 
 
 
 try { 
  String db = "jdbc:odbc:db1"; 
  conn = DriverManager.getConnection (db, "","");
  st = conn.createStatement();
  rs = st.executeQuery("Select * from emp");
  while(rs.next())
  {
    
   System.out.println(rs.getString(1));
  }
  System.out.println ("*** Connected to the database ***"); 

    
 } 
 catch (SQLException E) { 
  System.out.println ("SQLException: " + E.getMessage()); 
  System.out.println ("SQLState: " + E.getSQLState()); 
  System.out.println ("VendorError: " + E.getErrorCode()); 
 } 
 try { 
  st.close();
                if(conn!=null)
                {
                     conn.close (); 
                } 
 } 
 catch (SQLException E) {
  System.err.println ("Unable to load driver.");
  E.printStackTrace ();
 } 
}

}

No comments:

Post a Comment