





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This handout provides an overview of java database connectivity (jdbc) and guides students on how to connect and communicate with a microsoft access database using jdbc. It covers creating a database, setting up a system dsn, and the basic steps in using jdbc to establish a connection, create a statement, execute a query, process results, and close the connection.
Typology: Lecture notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Lecture 14
Java Database Connectivity (JDBC) provides a standard library for accessing databases. The JDBC API contains number of interfaces and classes that are extensively helpful while communicating with a database.
The java.sql package contains basic & most of the interfaces and classes. You automatically get this package when you download the J2SE™. You have to import this package whenever you want to interact with a relational database.
In this handout, we will learn how to connect & communicate with Microsoft Access Database. We chooses Access because most of you are familiar with it and if not than it is very easy to learn.
In start create a database “PersonInfo” using Microsoft Access. Create one table named “Person”. The schema of the table is shown in the picture.
Press Add… button and choose Microsoft Access Driver (*.mdb) from Create New Data Source window and press Finish button as shown in diagram.
After that, ODBC Microsoft Access Setup window would be opened as shown in following diagram
Enter the Data Source Name personDSN and select the database by pressing Select button. The browsing window would be opened, select the desired folder that contains the database (The database .mdb file you have created in the first step) Press Ok button.
There are eight (8) basic steps that must be followed in order to successfully communicate with a database. Let’s take a detail overview of all these one by one.
1. Import Required Package
Import the package java.sql.* that contains useful classes and interfaces to access & work with database. import java.sql.*;
2. Load Driver
Need to load suitable driver for underlying database. Different drivers & types for different databases are available. For MS Access, load following driver available with j2se. Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); For Oracle, load the following driver. You have to download it explicitly. Class.forName(“oracle.jdbc.driver.OracleDriver”);
3. Define Connection URL
To get a connection, we need to specify the URL of a database (Actually we need to specify the address of the database which is in the form of URL) As we are using Microsoft Access database and we have loaded a JDBC-ODBC driver. Using JDBC-ODBC driver requires a DSN which we have created earlier and named it personDSN. So the URL of the database will be String conURL = “jdbc:odbc:personDSN”;
4. Establish Connection With DataBase
Use DriverManager to get the connection object.
The URL of the database is passed to the getConnection method. Connection con = DriverManager.getConnection(conURL);
If DataBase requires username & password, you can use the overloaded version of getConnection method as shown below:
String usr = “umair”; String pwd = “vu”;
Always remember to call next() method at-least once
To retrieve the data of the column of the current row you need to use the various getters provided by the ResultSet. For example, the following code snippet will iterate over the whole ResultSet and illustrates the usage of getters methods while ( rs.next() ){ //by using column name String name = rs.getString(“columnName”); // or by using column index String name = rs.getString(1); }
8. Close the Connection
An opening connection is expensive, postpone this step if additional database operations are expected con.close();
The JdbcEx.java demonstrates the usage of all above explained steps. In this code example, we connect with the PersonInfo database, the one we have created earlier, and then execute the simple SQL SELECT query on Person table, and then process the query results.
// File JdbcEx.java
//step 1: import package import java.sql.*;
public class JdbcEx {
public static void main (String args[ ]) {
try {
//Step 2: load driver Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//Step 3: define the connection URL String url = “jdbc:odbc:personDSN”; //Step 4: establish the connection Connection con = DriverManager.getConnection(url); //Step 5: create Statement Statement st = con.createStatement();
//Step 6: preapare & execute the query String sql = “SELECT * FROM Person”; ResultSet rs = st.executeQuery(sql); //Step 7: process the results while(rs.next()){
// The row name is “name” in database “ PersonInfo , // hence specified in the getString() method. String name = rs.getString(“name”); String add = rs.getString(“address”); String pNum = rs.getString(“phoneNum”); System.out.println(name + “ ” + add + ” ” + pNum); }
//Step 8: close the connection con.close();
}catch(Exception sqlEx){ System.out.println(sqlEx); } } // end main } // end class