In This Session, You Will Learn To:: Objectives
In This Session, You Will Learn To:: Objectives
Objectives
Database Connectivity:
Sun Microsystems has included JDBC API as a part of J2SDK
to develop Java applications that can communicate with
databases.
The following figure shows the Airline Reservation System
developed in Java interacting with the Airlines database using
the JDBC API:
JDBC Architecture:
Provides the mechanism to translate Java statements into SQL
statements.
Can be classified into two layers:
JDBC application layer
JDBC driver layer
JDBC Drivers:
Convert SQL statements into a form that a particular database
can interpret.
Retrieve the result of SQL statements and convert the result
into equivalent JDBC API class objects.
Are of four types:
JDBC-ODBC Bridge driver
Native-API Partly-Java driver
JDBC-Net Pure-Java driver
Native Protocol Pure-Java driver
Connecting to a Database:
The DriverManager class provides the getConnection()
method to create a Connection object.
The getConnection() method method has the following
three forms:
Connection getConnection (String <url>)
Connection getConnection (String <url>, String
<username>, String <password>)
Connection getConnection (String <url>,
Properties <properties>)
boolean first() Shifts the control of a result set cursor to the first row
of the result set.
boolean isFirst() Determines whether the result set cursor points to the
first row of the result set.
boolean Shifts the control of a result set cursor before the first
beforeFirst() row of the result set.
boolean Determines whether the result set cursor points
isBeforeFirst() before the first row of the result set.
boolean last() Shifts the control of a result set cursor to the last row
of the result set.
boolean isLast() Determines whether the result set cursor points to the
last row of the result set.
Method Description
boolean Shifts the control of a result set cursor after the last row of
afterLast() the result set.
boolean Determines whether the result set cursor points after the
isAfterLast() last row of the result set.
boolean Shifts the control of a result set cursor to the previous row
previous() of the result set.
boolean Shifts the control of a result set cursor to the row number
absolute(int i) that you specify as a parameter.
void updateRow() Updates a row of the current ResultSet object and the
underlying database table.
void insertRow() Inserts a row in the current ResultSet object and the
underlying database table.
void deleteRow() Deletes a row from the current ResultSet object and the
underlying database table.
void Updates the specified column with the given string value.
updateString()
void updateInt() Updates the specified column with the given int value.
Problem Statement:
Create an application to retrieve information (author id, name,
phone, address, city, state, and zip ) about the authors who are
living in the city where the city name begins with the letter “S”.
Solution:
Type 4 driver is to be used for creating the application. To
solve the above problem, perform the following tasks:
Create a Data Source Name (DSN).
Code the application.
Compile and execute the application.
You can insert, update, and delete data from a table using the
DML statements in Java applications.
You can create, alter, and drop tables from a database using
the DDL statements in Java applications.
A ResultSet object stores the result retrieved from a database
when a SELECT statement is executed.
You can create various types of ResultSet objects such as
read only, updatable, and forward only.