Module 5
Module 5
Access
Dept.of CSE DSU
SYLLABUS
MODULE 5: 8
hrs
Database Access: The Concept of JDBC; JDBC Driver Types; JDBC Packages; A Brief Overview of the JDBC
process; Database Connection; Associating the JDBC/ODBC Bridge with the Database; Statement Objects;
Result Set.
▪ JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and
execute the query with the database. It is a part of JavaSE (Java Standard Edition).
JDBC API uses JDBC drivers to connect with the database.
▪ J2EE(Java 2 Platform Enterprise Edition) is a platform-independent, Java-centric
environment from Sun/Oracle for developing, building and deploying Web-based
enterprise applications online. The J2EE platform consists of a set of services, APIs,
and protocols that provide the functionality for developing multi-tiered, Web-based
applications.
▪ Open Database Connectivity (ODBC) is an open standard Application Programming
Interface (API) for accessing a database
▪ The JDBC driver makes J2EE components database independent, which complements Java's
philosophy of platform independence.
▪ Today there are JDBC drivers for nearly every commercial DBMS, and they are available from the
Sun Microsystems, Inc. website (www.sun.com) or from the DBMS manufacturer's web site.
▪ Java code independence is also extended to implementation of the SQL queries. SQL queries are
passed from the JDBC API through the JDBC driver to the DBMS without validation. This means it
is the responsibility of the DBMS to implement SQL statements contained in the query.
9
Dept.of CSE DSU
JDBC PACKAGES
▪ The JDBC-ODBC driver receive messages from a J2EE component that confirms to the JDBC specification and
those messages are translated by JDBC-ODBC driver into ODBC message format understood by DBMS.
▪ JDBC-ODBC Bridge is not suitable for mission critical applications as extra translation might negatively impact
the performance.
Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.
Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.
14
Dept.of CSE DSU
TYPE 1:JDBC DRIVER TYPES
Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.
Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.
15
Dept.of CSE DSU
TYPE 1:JDBC DRIVER TYPES
Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.
Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.
16
Dept.of CSE DSU
TYPE 1:JDBC DRIVER TYPES
Advantages:
1.It is very easy to use as once ODBC driver is installed.
2.Almost any database is supported.
Limitations:
1.Performance is not efficient.
2.ODBC drivers needs to be installed on the client machine.
3.Type 1 drivers are not portable as they are not completely written in Java.
17
Dept.of CSE DSU
JDBC-ODBC BRIDGE DRIVER
▪ The Java/Native Code driver uses Java classes to generate platform-specific code-that is, code only understood
by a specific DBMS.
▪ The manufacturer of the DBMS provides both the Java/Native Code driver and API classes so the J2EE
component can generate the platform-specific code.
▪ The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into
native calls of the database API. It is not written entirely in java.
▪ The obvious disadvantage of using a Java/Native Code driver is the loss of some portability of code. The API
classes for the Java/Native Code driver probably won't work with another manufacturer's DBMS.
Advantage:
▪ performance upgraded than JDBC-ODBC bridge driver.
▪ Disadvantage:
▪ The Native driver needs to be installed on the each client machine.
▪ The Vendor client library needs to be installed on client machine.
▪ Client side library is not available for all databases.
20
Dept.of CSE DSU
TYPE 2:NATIVE CODE/API DRIVER
▪ The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or
indirectly into the vendor-specific database protocol. It is fully written in java.
Advantage:
▪No client side library is required because of application server that can perform many tasks like
auditing, load balancing, logging etc.
Disadvantages:
▪Network support is required on client machine.
▪ Requires database-specific coding to be done in the middle tier.
▪ Maintenance of Network Protocol driver becomes costly because it requires database-specific
coding to be done in the middle tier.
23
Dept.of CSE DSU
TYPE 3:NETWORK PROTOCOL
DRIVER/MIDDLEWARE
Advantage:
▪ Better performance than all other drivers.
▪ No software is required at client side or server side.
Disadvantage:
▪ Drivers depend on the Database.
26
Dept.of CSE DSU
TYPE 4: JDBC DRIVER-THIN DRIVER
❏ The JDBC driver must be loaded before the J2EE component can connect to the DBMS.
❏ The Class.forName() method is used to load the JDBC driver.
❏ Suppose a developer wants to work offline and write a J2EE component that interacts with
Microsoft Access on the developer's PC.
❏ The developer must write a routine that loads the JDBC/ODBC Bridge driver called
sun.jdbc.odbc.jdbcOdbcDriver.
❏ The driver is loaded by calling the Class.forName() method and passing it the name of the driver,
as shown in the following code segment:
class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”)
Statement DataRequest;
ResultSet Results;
try {
String query = "SELECT * FROM Customers";
DataRequest = Database.createStatement();
DataRequest = Db.createStatement();
Results = DataRequest.executeQuery (query);
DataRequest.close();
Dept.of CSE DSU
}
Process the Data Returned by DBMS:
❏ The java.sql.ResultSet Object is assigned the results received from the DBMS after the query is
processed. The java.sql.ResultSet object consists of methods used to interact with the data that is
returned by the DBMS to the J2EE component
❏ The connection to the DBMS is terminated by using the close() method of the Connection
object once the J2EE component is finished accessing the DBMS.
❏ The close() method throws an exception if a problem is encountered when disengaging the
DBMS.
❏ DB.close()
Statement object, you can use to execute an SQL statement with one of its three execute methods:
▪ boolean execute (String SQL):
• to execute SQL DDL statements
▪ int executeUpdate (String SQL) :
• INSERT, UPDATE, or DELETE and DDL statement
• Returns the number of rows affected
▪ ResultSet executeQuery (String SQL) :
• Returns a ResultSet object for SELECT statement
Dept. of CSE, DSU 44
Creating Statement Object
The next code snippet shows how to employ the Connection.prepareCall() method
to instantiate a CallableStatement object based
▪ The ResultSet object contains methods that are used to copy data from the ResultSet into a Java
collection object or variable for further processing.
▪ Data in a ResultSet object is logically organized into a virtual table consisting of rows and columns. In
addition to data, the ResultSet object also contains metadata such as column names, column size, and
column data type.
▪ The ResultSet uses a virtual cursor to point to a row of the virtual table. A J2EE component must move
the virtual cursor to each row and then use other methods of the ResultSet object to interact with the data
stored in columns of that row.
▪ The virtual cursor is positioned above the first row of the data when the ResultSet is returned by the
executeQuery() method. So virtual cursor must be moved to first row using next() method.
▪ Once the virtual cursor points to arrow, the getXXX() method is used to copy data from the row to variable.
▪ The getxxx() method is data type specific. getString() method is used to copy String data from a column of the
ResultSet. The data type of the getxxx() method must be the same data type as the column in the ResultSet.
▪ The gebxxx() method requires one parameter, which is an integer that represents the number of the column that
contains the data. For example, getString(1) copies the data from the first column of the ResultSet.
▪ Columns appear in the ResultSet in the order in which column names appeared in the SELECT statement in the
query.
▪ SELECT statement: SELECT Customer FirstName, Customer LastName FROM Customer. This query directs the
DBMS to return two columns. The first column contains customer first names and the second column contains
customer last names. Therefore, getString(1) returns data in the customer first-name column of the current row in
the ResultSet.
READING THE RESULTSET
Example:
DataRequest=Db.createStatement();
do
System.out.println(printrow);
}while (Results.next());
▪ Six methods of the ResultSet object are used to position the virtual cursor, in addition to the next() method .These
are first(), last(), previous(), absolute(), relative(), and getRow().
▪ The first() method moves the virtual cursor to the first row in the ResultSet. Likewise, the last() method positions
the virtual cursor at the last row in the ResultSet. The previous() method moves the virtual cursor to the previous
row.
▪ The absolute() method positions the virtual cursor at the row number specified by the integer passed as a parameter
to the absolute() method . The relative() method moves the virtual cursor the specified number of rows contained in
the parameter. The parameter is a positive or negative integer where the sign represents the direction the virtual
cursor is moved.
▪ The Statement object that is created using the createStatement() of the Connection object must be set up to handle a
scrollable ResultSet by passing the createStatement() method one of three constants . These constants are
TYPE_FORWARD_ONLY,TYPE_SCROLL_INSENSITIVE and TYPE SCROLL SENSITIVE.
SCROLLABLE RESULTSET
The TYPE FORWARD ONLY constant restricts the virtual cursor to downward movement, which is
the default setting.
The TYPE_SCROLL_INSENSITIVE, and TYPE SCROLL SENSITIVE constants permit the virtual
cursor to move in both directions.
The TYPE-SCROLL SENSITIVE constant makes the ResultSet sensitive to those changes.
do{ Results.first();
Results.last();
Results.previous();
Results.absolute(10);
Results.relative(-2);
Results.relative(2);
System.out.println(printrow)
}while (Results.next());
▪ Alternatively, the CONCUR_READ ONLY constant can be passed to the createStatement() method to prevent the
ResultSet from being updated.
▪ There are three ways to update a ResultSet. These are updating values in a row, deleting a row, and inserting a new row.
▪ updateXXX() method is used to change the value of a column in the current row of the ResultSet. A value in a column of
the ResultSet can be replaced with a NULL value by using the updateNull() method.
Ex:
DataRequest=Db.createStatement(ResultSet.CONCUR_UPDATABLE);
Results.updateRow();
▪ Sometimes this is advantageous when processing the ResultSet because this is a way to eliminate
rows from future processing. For example, each row of a ResultSet may have to pass three tests.
Those that fail to pass the first test could be deleted from the ResultSet, thereby reducing the
number of rows in the ResultSet that have to be evaluated for the second test. This also deletes it
from the underlying database.
▪ The deleteRow() method is passed an integer that contains the number of the row to be deleted.
▪ The deleteRow() method is then passed a zero integer indicating that the current row must be
deleted Ex:Results.deleteRow(0);
▪ That is, the updateXXX() method is used to specify the column and value that will be placed into
the column of the ResultSet. The updateXXX() method requires two parameters. The first
parameter is either the name of the column or the number of the column of the ResultSet. The
second parameter is the new value that will be placed in the column of the ResultSet.
▪ The insertRow() method is called after the updateXXX() methods which causes anew row to be
inserted into the ResultSet having values that reflect the parameters in the updateXXX()
methods. This also updates the underlying database.