0% found this document useful (0 votes)
2 views3 pages

JDBC Java

The document contains Java code examples demonstrating how to connect to a Microsoft Access database using JDBC. It includes two classes: 'MsAccessDatabaseConnectionExample' which retrieves and displays player data from a database, and 'DBConnect' which establishes a connection to the database. Both examples handle exceptions and ensure proper resource management by closing connections after use.

Uploaded by

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

JDBC Java

The document contains Java code examples demonstrating how to connect to a Microsoft Access database using JDBC. It includes two classes: 'MsAccessDatabaseConnectionExample' which retrieves and displays player data from a database, and 'DBConnect' which establishes a connection to the database. Both examples handle exceptions and ensure proper resource management by closing connections after use.

Uploaded by

shwetikesoe704
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like