JAVA2
JAVA2
com/connectivity-with-access-without-dsn
Java is mostly used with Oracle, mysql, or DB2 database. So you can learn this topic only for knowledge.
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.
import java.sql.*;
class Test{
public static void main(String ar[]){
try{
String database="student.mdb";//Here database exists in the current directory
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(url);
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from login");
while(rs.next()){
System.out.println(rs.getString(1));
}
}catch(Exception ee){System.out.println(ee);}
}}
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.
import java.sql.*;
class Test{
public static void main(String ar[]){
try{
String url="jdbc:odbc:mydsn";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(url);
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from login");
while(rs.next()){
System.out.println(rs.getString(1));
}
}catch(Exception ee){System.out.println(ee);}
}}
Feedback
Preparation
Trending Technologies
B.Tech / MCA