Java 4 TH
Java 4 TH
JDBC Driver
JDBC Driver is a software component that enables java application to interact with the
database. There are 4 types of JDBC drivers:
1) JDBC-ODBC bridge driver (Bridge): This driver uses ODBC driver to connect to the database.
It converts JDBC method calls into the ODBC function calls. It is also called Universal driver
because it can be used to connect to any of the databases.
2) Native-API driver (Native): The Native API driver uses the client -side libraries of the
database. This driver converts JDBC method calls into native calls of the database API. In
order to interact with different database, this driver needs their local API.
3) Network Protocol driver (Middleware): This driver translates the JDBC calls into a database
server independent and Middleware server-specific calls. Middleware server further
translates JDBC calls into database specific calls.
4) Thin driver (Pure): This driver is called Pure Java Driver because this driver interacts directly
with database. It does not require any native database library that is why it is also known as
Thin Driver.
The JDBC API supports two-tier and three-tier architecture for database access.
In a two-tier model, a Java application/applet communicates directly with the database, via the
JDBC driver. The Java application/applet and the database can be on the same machine, or the
database can be on a server and the Java application/applet can be on a client machine using
any network protocol.
In a three-tier model, a Java application/applet communicates with a middle tier component
that functions as an application server. In the three-tier model, commands are sent to a middle
tier of services, which then send SQL statements to the database. The database processes the
SQL statements and sends the results back to the middle tier, which then sends them to the
user.
The fundamental steps involved in the process of connecting to a database and executing a
query consist of the following:
This is for making the JDBC API classes immediately available to the application program. The
following import statement should be included in the program irrespective of the JDBC driver
being used:
import java.sql.*;
In this step we load the JDBC driver class into JVM. This step is also called as registering the
JDBC driver. The forName() method of class Class is used to register the driver class. This
method is used to dynamically load the driver class. This step can be completed in two ways.
Example: Class.forName("oracle.jdbc.driver.OracleDriver");
DriveManager.registerDriver(object of driver class)
In this step connection between a java program and a database will be opened. To open the
connection, we call getConnection() method of DriverManager class.
url
username
password
url: url is used to select one register JDBC driver among multiple registered driver by
DriverManager class.
username and password: username and password are used for authentication purpose.
Example:
To transfer sql commands from java program to database we need statement object. To create
a statement object we call createStatement() method of connection interface. The object of
statement is responsible to execute queries with the database.
Example:
Once a Statement object has been constructed, the next step is to execute the query.
Call any one of the following three methods of Statement interface is used to execute queries
to the database and to get the output.
These methods return the object of ResultSet that can be used to get all the records of a table.
Example:
while(rs.next())
System.out.println(rs.getString(1)+" "+rs.getString(2));
Once the query has been executed, there are two steps to be carried out:
The first step is done using the next() method of the ResultSet object.
The second step is done by using the getXXX() methods of the JDBC rset object. Here getXXX()
corresponds to the getInt(), getString() etc with XXX being replaced by a Java datatype.
String str;
while (rset.next())
fp.write(buf);
fp.close();
Once the ResultSet and Statement objects have been used, they must be closed explicitly. This
is done by calls to the close() method of the ResultSet and Statement classes. The following
code illustrates this:
rset.close();
sql_stmt.close();
The last step is to close the database connection opened in the beginning after importing the
packages and loading the JDBC drivers. This is done by a call to the close() method of the
Connection class.
conn.close();
ResultSet also an interface and is used to retrieve SQL select query results. A default ResultSet
object is not updatable and the cursor moves only in forward direction.
The methods of the ResultSet interface can be broken down into three categories −
Get methods: Used to view the data in the columns of the current row being pointed by the
cursor.
Update methods: Used to update the data in the columns of the current row. The updates can
then be updated in the underlying database as well.
1) public boolean next(): is used to move the cursor to the one row next from the current
position.
2) public boolean previous(): is used to move the cursor to the one row previous from the
current position.
3) public boolean first(): is used to move the cursor to the first row in result set object.
4) public boolean last(): is used to move the cursor to the last row in result set object.
5) public boolean absolute(int is used to move the cursor to the specified row number in the
row): ResultSet object.
6) public boolean relative(int is used to move the cursor to the relative row number in the
row): ResultSet object, it may be positive or negative.
7) public int getInt(int is used to return the data of specified column index of the current
columnIndex): row as int.
8) public int getInt(String is used to return the data of specified column name of the current
columnName): row as int.
9) public String getString(int is used to return the data of specified column index of the current
columnIndex): row as String.
10) public String getString(String is used to return the data of specified column name of the current
columnName): row as String.
ResultSetMetaData Interface
The metadata means data about data i.e. we can get further information from the data.
Syntax:
ResultSetMetaData rsmd=rs.getMetaData();
Commonly used methods of ResultSetMetaData interface
It provides classes that are fundamental to the design of the Java programming language. The
most important classes are Object, which is the root of the class hierarchy, and Class, instances
of which represent classes at run time.
It contains the collections framework, legacy collection classes, event model, date and time
facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-
number generator, and a bit array).
This package provides for system input and output through data streams, serialization and the
file system. Unless otherwise noted, passing a null argument to a constructor or method in any
class or interface in this package will cause a NullPointerException to be thrown.
Java.lang package in Java
Java.util Package in Java
Java.io Package in Java