All for Joomla All for Webmasters

Connectivity with Access without DSN

There are two ways to connect java application with the access database.

  1. Without DSN (Data Source Name)
  2. With DSN

Java is mostly used with Oracle, mysql, or DB2 database. So you can learn this topic only for knowledge.


Example to Connect Java Application with access without DSN

In this example, we are going to connect the java program with the access database. In such case, we have created the login table in the access database. There is only one column in the table named name. Let’s get all the name of the login table.

  1. import java.sql.*;
  2. class Test{
  3. public static void main(String ar[]){
  4.  try{
  5.    String database=“student.mdb”;//Here database exists in the current directory
  6.    String url=”jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};
  7.                     DBQ=” + database + “;DriverID=22;READONLY=true“;
  8.    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
  9.    Connection c=DriverManager.getConnection(url);
  10.    Statement st=c.createStatement();
  11.    ResultSet rs=st.executeQuery(“select * from login”);
  12.    while(rs.next()){
  13.     System.out.println(rs.getString(1));
  14.    }
  15. }catch(Exception ee){System.out.println(ee);}
  16. }}

Example to Connect Java Application with access with DSN

Connectivity with type1 driver is not considered good. To connect java application with type1 driver, create DSN first, here we are assuming your dsn name is mydsn.

  1. import java.sql.*;
  2. class Test{
  3. public static void main(String ar[]){
  4.  try{
  5.    String url=“jdbc:odbc:mydsn”;
  6.    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
  7.    Connection c=DriverManager.getConnection(url);
  8.    Statement st=c.createStatement();
  9.    ResultSet rs=st.executeQuery(“select * from login”);
  10.    while(rs.next()){
  11.     System.out.println(rs.getString(1));
  12.    }
  13. }catch(Exception ee){System.out.println(ee);}
  14. }}
PinIt
submit to reddit

Leave a Reply

Top