0% found this document useful (0 votes)
16 views6 pages

JAVA2

This document discusses two ways to connect a Java application to an Access database: without a DSN and with a DSN. It provides an example of connecting without a DSN by specifying the database file location and driver information in the connection URL. It also briefly mentions connecting with a DSN assumes the DSN name "mydsn" has already been created.

Uploaded by

水仙 水仙
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

JAVA2

This document discusses two ways to connect a Java application to an Access database: without a DSN and with a DSN. It provides an example of connecting without a DSN by specifying the database file location and driver information in the connection URL. It also briefly mentions connecting with a DSN assumes the DSN name "mydsn" has already been created.

Uploaded by

水仙 水仙
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Connectivity with access without dsn - javatpoint https://www.javatpoint.

com/connectivity-with-access-without-dsn

Home Java Programs OOPs String Exception Multithreading Collections

1 sur 6 14/12/2023, 13:55


Java Connectivity with access without dsn - javatpoint https://www.javatpoint.com/connectivity-with-access-without-dsn

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.

import java.sql.*;
class Test{
public static void main(String ar[]){
try{
String database="student.mdb";//Here database exists in the current directory

String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};


DBQ=" + database + ";DriverID=22;READONLY=true";

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);}

}}

download this example

Example to Connect Java Application with access with DSN

2 sur 6 14/12/2023, 13:55


Java Connectivity with access without dsn - javatpoint https://www.javatpoint.com/connectivity-with-access-without-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.

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);}

}}

3 sur 6 14/12/2023, 13:55


Java Connectivity with access without dsn - javatpoint https://www.javatpoint.com/connectivity-with-access-without-dsn

For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

Help Others, Please Share

Learn Latest Tutorials

Preparation

4 sur 6 14/12/2023, 13:55


Java Connectivity with access without dsn - javatpoint https://www.javatpoint.com/connectivity-with-access-without-dsn

Trending Technologies

B.Tech / MCA

5 sur 6 14/12/2023, 13:55


Java Connectivity with access without dsn - javatpoint https://www.javatpoint.com/connectivity-with-access-without-dsn

6 sur 6 14/12/2023, 13:55

You might also like