Accessing Ms Access Using Java

by : Twinkle



Step by step guide:

1.Create a database in ms access and add a table
2.Add columns to the table.
3.Go to control panel->Administrative Tools
->JDBC-> System DSN
4.In System DSN tab click on add ,choose
Microsoft access driver ,then you will get a
dialog box opened ODBC Microsoft access setup
mention the data source name in it and in
databases click on select button and choose
your .mdb file that is created in step1.

5.Register the driver using

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

6.Connect to the database

Connection con = DriverManager.getConnection
(URL,userid,password);
url=" Jdbc:Odbc:"

userid and password are empty in case of MS access.if it oracle default userid is "scott" password is "tiger".

Ex: Connection con = DriverManager.getConnection
( "Jdbc:Odbc:msac", "","");

Source Code

import java.sql.*;
import java.io.*;

class JdbcDemo1
{
Connection con;
Statement stmt;
ResultSet rs;
JdbcDemo1(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection( "Jdbc:Odbc:msac", "","");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM Table1");
while (rs.next()) {
String x = rs.getString("name");
int s = rs.getInt("age");
System.out.println("x:"+x+"s"+s);
}
}catch(SQLException e){
System.out.println("Hello World!"+e);
}catch(ClassNotFoundException e){
System.out.println("purni"+e);
}
}
}

class JdbcDemo
{

public static void main(String args[]){

new JdbcDemo1();
}
}