To connect a Java application with Access database using JDBC-ODBC Bridge Driver. You need to follow the following steps
import java.sql.*;
class Test
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
Statement s=con.createStatement(); //creating statement
ResultSet rs=s.executeQuery("select * from student"); //executing statement
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}
}
}
Create DSN Name:
1.Go to Control panel:
2.Go to Administrative tools:
3.Select datasource(ODBC):
4.Add New DSN Name,select Add:
5.Select Access driver from the list,click on finish:
6.Give a DSN name, click ok:
Example:
We suppose that you have created a student table with sid and name column name in access database.
class Test
{
public static void main(String []args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "", "");
Statement s=con.createStatement(); //creating statement
ResultSet rs=s.executeQuery("select * from student"); //executing statement
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
con.close(); //closing connection
}catch(Exception e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment