import [Link].
Connection;
import [Link];
import [Link];
import [Link];
import [Link];
public class MsAccessDatabaseConnectionExample {
public static void main(String[] args) {
// variables
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
// Step 1: Loading or
// registering Oracle JDBC driver class
try {
[Link]("[Link]");
}
catch(ClassNotFoundException cnfex) {
[Link]("Problem in loading"
+ " MS Access JDBC driver");
[Link]();
}
// Step 2: Opening database connection
try {
String msAccessDBName = "D:\\WORKSPACE"
+ "\\TEST_WORKSPACE\\Java-JDBC\\[Link]";
String dbURL = "jdbc:odbc:Driver="
+ "{Microsoft Access Driver (*.mdb, *.accdb)};"
+ "DBQ="
+ msAccessDBName
+ ";DriverID=22;READONLY=true";
// Step 2.A: Create and
// get connection using DriverManager class
connection = [Link](dbURL);
// Step 2.B: Creating JDBC Statement
statement = [Link]();
// Step 2.C: Executing SQL and
// retrieve data into ResultSet
resultSet = statement
.executeQuery("SELECT * FROM PLAYER");
[Link]("ID\tName\t\t\tAge\tMatches");
[Link]("==\t================\t===\t=======");
// processing returned data and printing into console
while([Link]()) {
[Link]([Link](1) + "\t" +
[Link](2) + "\t" +
[Link](3) + "\t" +
[Link](4));
}
}
catch(SQLException sqlex){
[Link]();
}
finally {
// Step 3: Closing database connection
try {
if(null != connection) {
// cleanup resources, once after processing
[Link]();
[Link]();
// and then finally close connection
[Link]();
}
}
catch (SQLException sqlex) {
[Link]();
}
}
}
}
Output:
1
2ID
==
Name
================
Age Matches
=== =======
31 Sachin Tendulkar 43 200
42 Shane Warne 45 145
53 Kevin Pietersen 36 104
64 Shahid Afridi 36 27
5 Brian Lara 46 131
76 Graeme Smith 36 117
87 Mahela Jayawardene 38 145
9
import [Link].*;
public class DBConnect {
public DBConnect() {
try {
[Link]("[Link]");
[Link]("DriverLoaded");
String url = "jdbc:odbc:; DRIVER = Microsoft Access Driver (*.mdb,
*.accdb); DBQ = [Link]";
Connection con = [Link](url);
[Link]("Connection Established Successfully");
} catch(Exception e) {
[Link]();
[Link]("Could Not Connect to Database");
}
}
public static void main (String args[]) {
DBConnect dbcon = new DBConnect();
}
}