Advance Java Module 5 Notes
Advance Java Module 5 Notes
2.JDBC Drivers
To connect with individual databases, JDBC requires drivers for each database.
The various driver types are described in the following sections:
Type I: JDBC-ODBC Bridge
Type II: Native API/JAVA
Type III: Pure Java driver for database middleware(JDBC protocol)
Type Four Driver : Direct-to-database pure Java driver (JAVA protocol)
Type 1: JDBC-ODBC Bridge Driver
In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client
machine. Using ODBC, requires configuring on your system a Data Source Name (DSN)
that represents the target database.
When Java first came out, this was a useful driver because most databases only supported
ODBC access but now this type of driver is recommended only for experimental use or
when no other alternative is available.
Define JDBC
List the four different types of JDBC drivers.
List out the difference between JDBC and ODBC
Class Room Session 2
Advantages for using this type of driver include the following:
Allows access to almost any database since the databases ODBC drivers are readily
available
Offers significantly better performance than the JDBC/ODBC Bridge and Type 2 Drivers
Advanced Java feature set
Scalable
Caching
Advanced system administration
Does not require applicable database client libraries
The disadvantage for using this type of driver is that it requires a separate JDBC middleware
server to translate specific native-connectivity interface.
Type 4: Pure Java protocal
In a Type 4 driver, a pure Java-based driver communicates directly with the vendor's
database through socket connection. This is the highest performance driver available for
the database and is usually provided by the vendor itself.
This kind of driver is extremely flexible, you don't need to install special software on the
client or server. Further, these drivers can be downloaded dynamically.
5.Database connection:
The setXXX() methods bind values to the parameters, where XXX represents the Java
data type of the value you wish to bind to the input parameter.
o setXXX(int,string);
First parameter represent the column index and second parameter represent the values
that replace the ? mark in the query.
Next different execut methods of the preparedStatement object are called.
import java.sql.*;
class A
{
A()
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection c=DriverManager.getConnection(“JDBC:ODBC:CSB”);
PreparedStatement p=c.PreaparedStatement(“select name from emp where usn=?”);
p.setSting(2, ”12cs001”);
ResultSet r=p.executeQuery();
Program:
import java.sql.*;
class A
{
A()
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection c=DriverManager.getConnection(“JDBC:ODBC:CSB”);
CallableStatement p=c.CallableSatement(“Call lastOrderNumber(?)”);
p.registerOutParameter(1,TYPES.VARCHAR);
p.executeQuery();
6. ResultSet
A ResultSet consists of records. Each records contains a set of columns.
A ResultSet can be of a certain type. The type determines some characteristics and abilities of the
ResultSet.
Scrollable ResultSet:
At the time of writing there are three ResultSet types:
1. ResultSet.TYPE_FORWARD_ONLY
2. ResultSet.TYPE_SCROLL_INSENSITIVE
3. ResultSet.TYPE_SCROLL_SENSITIVE
The default type is TYPE_FORWARD_ONLY
TYPE_FORWARD_ONLY means that the ResultSet can only be navigated forward. That is,
you can only move from row 1, to row 2, to row 3 etc. You cannot move backwards in
the ResultSet.
TYPE_SCROLL_INSENSITIVE means that the ResultSet can be navigated (scrolled) both
forward and backwards. You can also jump to a position relative to the current position,
Method Description
absolute() Moves the ResultSet to point at an absolute position. The position is a row
number passed as parameter to the absolute() method.
afterLast() Moves the ResultSet to point after the last row in the ResultSet.
beforeFirst() Moves the ResultSet to point before the first row in the ResultSet.
first() Moves the ResultSet to point at the first row in the ResultSet.
last() Moves the ResultSet to point at the last row in the ResultSet.
next() Moves the ResultSet to point at the next row in the ResultSet.
previous() Moves the ResultSet to point at the previous row in the ResultSet.
relative() Moves the ResultSet to point to a position relative to its current position. The
relative position is passed as a parameter to the relative method, and can be
both positive and negative.
PROGRAM:
import java.sql.*;
import java.sql.*;
class A
{
A()
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection c=DriverManager.getConnection(“JDBC:ODBC:CSB”);
releaseSavePoint(String);-it realse the save point assing to the sql statement if and only
rollback();-if one of the sql statement is failed,then rollback() method is invoked and
control goes back to the fail sql statement for further execution.
Program:
import java.sql.*;
class A
{
A()
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
8.Metadata:
Metadata is data about data. J2ee component can access metadata by using
DatabaseMetaData interface.
ResultSetMetaData interface
DatabaseMetaData interface:
The DatabaseMetaData interface is used to retrieve information about database,table,columns
and index amoung other information about dbms.
ResultSetMetaData interface:
ResultSetMetaData interface is used to retrieve the information by calling the getMetaData()
method of ResultSet interface.
Different methods in the ResultSetMetaData inetface are as follows:
getColunmCount()-returns the number of column available in the table
getColunmName(int)-returns the name of column specified by the column
number
getColunmTye(int)-returns the type of column specified by the column number
program:
import java.sql.*;
class A
{
A()
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection c=DriverManager.getConnection(“JDBC:ODBC:CSB”);
Statement s=c.createStatement();
ResultSet r=s.executeQuery(“Select *from emp”);
ResultSetMetaData d=r.getMetaData();
System.out.println(d.getColunmName(1));
Data Types:
The JDBC driver converts the Java data type to the appropriate JDBC type, before sending it to the
database. It uses a default mapping for most data types. The following table summarizes the default
JDBC data type that the Java data type is converted to, when you call the setXXX() method.
SQL JDBC/Java
VARCHAR String
CHAR String
LONGVARCHAR String
BIT boolean
NUMERIC java.math.BigDecimal
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT float
DOUBLE double
Exception :
SQLException Methods
An SQLException can occur both in the driver and the database. When such an exception occurs,
an object of type SQLException will be passed to the catch clause.
The passed SQLException object has the following methods available for retrieving additional
information about the exception −
Method Description
getErrorCode( ) Gets the error number associated with the exception.
Gets the JDBC driver's error message for an error,
getMessage( ) handled by the driver or gets the Oracle error number
and message for a database error.
Gets the XOPEN SQLstate string. For a JDBC driver
error, no useful information is returned from this
getSQLState( )
method. For a database error, the five-digit XOPEN
SQLstate code is returned. This method can return null.
getNextException( ) Gets the next Exception object in the exception chain.
Prints the current exception, or throwable, and it's
printStackTrace( )
backtrace to a standard error stream.
Prints this throwable and its backtrace to the print
printStackTrace(PrintStream s)
stream you specify.
Prints this throwable and it's backtrace to the print
printStackTrace(PrintWriter w)
writer you specify.
1. Describe the steps of JDBC process with suitable exception handling blocks.
2. What is ResultSet? How to get scroll options to ResultSet? Explain.
3. Write a note on Database metadata object methods and ResultSet metadata
object methods.
4. Explain J2EE multitier architecture with a neat diagram.
5. Explain the different types of statement object. Give example for each.
6. Explain the four types of JDBC drivers.
7. Describe the various steps of JDBC with code snippets.
Or
8. Explain what are the basic steps involved in database connection.