You are on page 1of 69

Lesson 6:

Database Programming
with Java

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Course Objective
JDBC Basics
Basic steps in using JDBC
Statement
ResultSet
PreparedStatement
JDBC Advanced
Handling SQLExceptions
Batch update
Transaction
RowSet
Stored Procedures
Connection pool

Guide

to use HSQL & MySQL

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Learning Approach

The following are strongly suggested for a better


learning and understanding of this course:
Noting down the key concepts in the class
Analyze all the examples / code snippets provided
Study and understand the self study topics
Completion and submission of all the assignments, on time
Completion of the self review questions in the lab guide
Study and understand all the artifacts including the reference
materials / e-learning / supplementary materials specified
Completion of the project (if application for this course) on time
inclusive of individual and group activities
Taking part in the self assessment activities
Participation in the doubt clearing sessions

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Session 1

JDBC BASICS

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

JDBC
JDBC (Java DataBase
Connectivity) provides a
standard library for
accessing relational
databases
JDBC consists of two parts:

JDBC API, a purely Java-based


API
o JDBC Driver Manager,which
communicates with vendorspecific drivers that perform
the real communication with
the database.
o

JDBC classes are in the


java.sql package

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Advantages of JDBC
Continued usage of existing data
Vendor independent
Platform independent
Ease of use

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

JDBC Driver type

FPT SOFTWARE TRAINING MATERIAL Internal use

Left side, Type 1: JDBC-ODBC Bridge


plus ODBC Driver
This combination provides JDBC
access via ODBC drivers. ODBC
binary code -- and in many cases,
database client code -- must be
loaded on each client machine that
uses a JDBC-ODBC Bridge. Sun
provides a JDBC-ODBC Bridge
driver, which is appropriate for
experimental use and for situations
in which no other driver is available.
Right side, Type 2: A native API
partly Java technology-enabled
driver
This type of driver converts JDBC
calls into calls on the client API for
Oracle, Sybase, Informix, DB2, or
other DBMS. Note that, like the
bridge driver, this style of driver
requires that some binary code be
loaded on each client machine.
04e-BM/NS/HDCV/FSOFT v2/3

JDBC Driver type (cont)

FPT SOFTWARE TRAINING MATERIAL Internal use

Right side, Type 3: Pure Java


Driver for Database Middleware
This style of driver translates
JDBC calls into the middleware
vendor's protocol, which is then
translated to a DBMS protocol by
a middleware server. The
middleware provides connectivity
to many different databases.
Left side, Type 4: Direct-toDatabase Pure Java Driver
This style of driver converts JDBC
calls into the network protocol
used directly by DBMSs, allowing
a direct call from the client
machine to the DBMS server and
providing a practical solution for
intranet access.
04e-BM/NS/HDCV/FSOFT v2/3

JDBC Driver Types

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Basic Steps in using JDBC

1. Load the driver


2. Define the connection URL and
establish the connection
3. Create a Statement object
4. Executing the Statement
5. Getting data from executing
result
6. Process the results.
7. Close the connection

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Loading JDBC drivers


DriverManager.registerDriver(new

DriverXYZ());
o
o

Create an instance of Driver class


Register with DriverManager

Class.forName("DriverXYZ");
o
o

Use Class.forName() to load the Driver class


No need to register with DriverManager.

How to specify the Driver class.


o

Refer to the driver documentation.

Some examples.

Class.forName("oracle.jdbc.driver.OracleDriv
er");
o Class.forName("org.gjt.mm.mysql.Driver");
o Class.forName(com.mysql.jdbc.Driver");
o Class.forName(com.ibm.db2j.jdbc.DB2jDriv
o

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Establish the DB connection


Need to know which database to connect to
o
o

In JDBC you need to specify the URL


Need username, password

Get a java.sql.Connection object


o

Connection con = DriverManager.getConnection(url, user,


password);

How to specify the URL.


o

Refer to the driver documentation.

Examples:
o
o

jdbc:oracle:thin:@localhost:1521:orcl
jdbc:mysql://127.0.0.1:3306/books

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

JDBC Statement Object

A Statement object is what sends


your SQL statement to the DBMS
Create a Statement object and then
execute it, supplying appropriate
method with the SQL statement you
want to send.
o

Statement stmt = con.createStatement();

For a SELECT statement, the method


to use is executeQuery.
o

stmt.executeQuery( SELECT * FROM XXX");

For statements that create or modify


tables, the method to use is
executeUpdate.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

ResultSet object
A ResultSet object is a table of data
representing a database result set,
which is usually generated by
executing a statement that queries the
database.
You access the data in a ResultSet
object through a cursor. This cursor is a
pointer that points to one row of data
in the ResultSet. Initially, the cursor is
positioned before the first row.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

ResultSet type

TYPE_FORWARD_ONLY: The result set cannot be scrolled; its


cursor moves forward only, from before the first row to after
the last row. The rows contained in the result set depend on
how the underlying database generates the results. That is,
it contains the rows that satisfy the query at either the time
the query is executed or as the rows are retrieved.
TYPE_SCROLL_INSENSITIVE: The result can be scrolled; its
cursor can move both forward and backward relative to the
current position, and it can move to an absolute position.
The result set is insensitive to changes made to the
underlying data source while it is open. It contains the rows
that satisfy the query at either the time the query is
executed or as the rows are retrieved.
TYPE_SCROLL_SENSITIVE: The result can be scrolled; its
cursor can move both forward and backward relative to the
current position, and it can move to an absolute position.
The result set reflects changes made to the underlying data
source while the result set remains open.
The default ResultSet type is TYPE_FORWARD_ONLY.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

ResultSet Concurrency
The concurrency of a ResultSet object
determines what level of update
functionality is supported.
There are two concurrency levels:

CONCUR_READ_ONLY: The ResultSet object


cannot be updated using the ResultSet
interface.
CONCUR_UPDATABLE: The ResultSet object
can be updated using the ResultSet interface.

The default ResultSet concurrency is


CONCUR_READ_ONLY.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Cursor Holdability

Calling the method Connection.commit can close


the ResultSet objects that have been created
during the current transaction. In some cases,
however, this may not be the desired behavior.
The ResultSet property holdability gives the
application control over whether ResultSet objects
(cursors) are closed when commit is called.
HOLD_CURSORS_OVER_COMMIT: ResultSet cursors are not
closed; they are holdable: they are held open when the
method commit is called. Holdable cursors might be ideal if
your application uses mostly read-only ResultSet objects.
CLOSE_CURSORS_AT_COMMIT: ResultSet objects (cursors)
are closed when the commit method is called. Closing
cursors when this method is called can result in better
performance for some applications.

The default cursor holdability varies depending on


your DBMS.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

ResultSet navigation
Method

Description

next()

Moves the cursor froward one row from its current


position.

previous()

Moves the cursor to the previous row in this ResultSet


object.

first()

Moves the cursor to the first row in this ResultSet


object.

last()

Moves the cursor to the last row in this ResultSet


object.

beforeFirst()

Moves the cursor to the front of this ResultSet object,


just before the first row. This method has no effect if
the result set contains no rows.

afterLast()

Moves the cursor to the end of this ResultSet object,


just after the last row. This method has no effect if the
result set contains no rows.

getRow()

Retrieves the current row number. The first row is


number 1, the second number 2, and so on.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

ResultSet navigation (cont)


Method

Description

absolute(int
row)

Moves the cursor to the given row number in this


ResultSet object.
If the row number is positive, the cursor moves to
the given row number with respect to the beginning of
the result set. The first row is row 1, the second is row
2, and so on.
If the given row number is negative, the cursor
moves to an absolute row position with respect to the
end of the result set. For example, calling the method
absolute(-1) positions the cursor on the last row;
calling the method absolute(-2) moves the cursor to
the next-to-last row, and so on.

relative(int
rows)

Moves the cursor a relative number of rows, either


positive (forward) or negative (backward).
Note: Calling the method relative(1) is identical to
calling the method next() and calling the method
relative(-1) is identical to calling the method
previous().
does not
v2/3
FPT SOFTWARE TRAINING MATERIAL
Internal use Calling relative(0) is valid, but 04e-BM/NS/HDCV/FSOFT

Methods of returning Scrollable


ResultSet

public Statement
createStatement(intresultSetType,
intresultSetConcurrency) throws SQLException
public PreparedStatement
prepareStatement(Stringsql,
intresultSetType, intresultSetConcurrency)
throws SQLException
public CallableStatement
prepareCall(Stringsql, intresultSetType,
intresultSetConcurrency) throws SQLException

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Retrieving Column Values from Rows

The ResultSet interface declares getter


methods (example: getBoolean, getLong, )
for retrieving column values from the current
row.
You can retrieve values using either the index
number of the column or the alias or name of
the column.
The column index is usually more efficient.
Columns are numbered from 1.
For maximum portability, result set columns
within each row should be read in left-to-right
order, and each column should be read only
once.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Updating Rows in ResultSet Objects

With CONCUR_UPDATABLE ResultSet,


you can update values using
updateXXX() method.
You can update values using either the
index number of the column or the
name of the column.
However, none of these updater
methods modifies the database; you
must call the method
ResultSet.updateRow to update the
database.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Updating a row

Step 1: Positioning the Cursor

Statement st =
cn.createStatement(ResultSet.TYPE_SCROLL_SENS
ITIVE, ResultSet.CONCUR_UPDATABLE)
ResultSet rs = st.executeQuery(SELECT NAME,
EMPLOEE_ID FROM EMPLOYEES);
rs.first();

Step 2: Updating the columns

rs.updateInt(2,2345); //rs.update<Type>

Step 3: Committing the update

rs.updateRow();

Scrollable Resultset and Rowsets


FPT SOFTWARE TRAINING MATERIAL Internal
use
/ Session2
/ 3 of 27

04e-BM/NS/HDCV/FSOFT v2/3

Inserting a row

Step 1: Positioning the Cursor

Statement st =
cn.createStatement(ResultSet.TYPE_SCROLL_SEN
SITIVE, ResultSet.CONCUR_UPDATABLE)
ResultSet rs = st.executeQuery(SELECT NAME,
EMPLOEE_ID FROM EMPLOYEES);
rs.first();

Step 2: Updating the columns

rs.update<Type>

Step 3: inserting a row

rs.insertRow();

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Deleting a row
Step 1: Positioning the cursor
// Move the cursor to the last row of the
result set

rs.last();

Step 2: Deleting the row


// Deleting the row from the result set

rs.deleteRow();

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Example: Inserting Rows


try {
stmt = con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = stmt.executeQuery(
"SELECT * FROM " + dbName + ".COFFEES");
uprs.moveToInsertRow();
uprs.updateString("COF_NAME", coffeeName);
uprs.updateInt("SUP_ID", supplierID);
uprs.updateFloat("PRICE", price);
uprs.updateInt("SALES", sales);
uprs.updateInt("TOTAL", total);
uprs.insertRow();
uprs.beforeFirst();
} catch (SQLException e ) {

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Connecting and Querying sample Database

DisplayAuthors
o
o

Retrieves the entire authors table


Displays the data in a JTextArea

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

DisplayAuthors.java
1 // DisplayAuthors.java
2 // Displaying the contents of the authors table.
3
4 import java.awt.*;
5 import java.sql.*;
6 import java.util.*;
7 import javax.swing.*;
8
9 public class DisplayAuthors extends JFrame {
10
11 // JDBC driver name and database URL
12 static String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
13 String DATABASE_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
14
15 // declare Connection and Statement for accessing
16 // and querying database
17 private Connection connection;
18 private Statement statement;
19
20 // constructor connects to database, queries database, processes
21 // results and displays results in window
22 public DisplayAuthors()
23 {
24 super( "Authors Table of Books Database" );
25

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

DisplayAuthors.java
26
27
28
29
30
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

// connect to database books and query database


try {

// load database driver class


Class.forName( JDBC_DRIVER );
// establish connection to database
connection = DriverManager.getConnection( DATABASE_URL );
// create Statement for querying database
statement = connection.createStatement();
// query database
ResultSet resultSet =
statement.executeQuery( "SELECT * FROM authors" );
// process query results
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

DisplayAuthors.java
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

for ( int i = 1; i <= numberOfColumns; i++ )


results.append( metaData.getColumnName( i ) + "\t" );
results.append( "\n" );
while ( resultSet.next() ) {
for ( int i = 1; i <= numberOfColumns; i++ )
results.append( resultSet.getObject( i ) + "\t" );
results.append( "\n" );
}
// set up GUI and display window
JTextArea textArea = new JTextArea( results.toString() );
Container container = getContentPane();
container.add( new JScrollPane( textArea ) );
setSize( 300, 100 ); // set window size
setVisible( true ); // display window
} // end try

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

DisplayAuthors.java
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

// detect problems interacting with the database


catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null, sqlException.getMessage(),
"Database Error", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
// detect problems loading database driver
catch ( ClassNotFoundException classNotFound ) {
JOptionPane.showMessageDialog( null, classNotFound.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
// ensure statement and connection are closed properly
finally {
try {
statement.close();
connection.close();
}

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

DisplayAuthors.java
98 // handle exceptions closing statement and connection
99 catch ( SQLException sqlException ) {
100 JOptionPane.showMessageDialog( null,
101 sqlException.getMessage(), "Database Error",
102 JOptionPane.ERROR_MESSAGE );
103
104 System.exit( 1 );
105 }
106 }
107
108 } // end DisplayAuthors constructor
109
110 // launch the application
111 public static void main( String args[] )
112 {
113 DisplayAuthors window = new DisplayAuthors();
114 window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
115 }
116
117 } // end class DisplayAuthors

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

PreparedStatement Object
An object that represents a precompiled
SQL statement.
PreparedStatement object is given an SQL
statement when it is created. This SQL
statement is sent to the DBMS right away,
where it will be compiled.
When the PreparedStatement is executed,
the DBMS can just run the
PreparedStatement 's SQL statement
without having to compile it first.
PreparedStatement objects are used more
for SQL statements that take parameters.
The advantage of using SQL statements that
take parameters is that you can use the
same statement and supply it with different

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

PreparedStatement Example
PreparedStatement pstmt = con.prepareStatement(
"UPDATE authors
SET lastName = ?
WHERE authorID = ?");
pstmt.setString(1, John);
pstmt.setInt(2, 3);
pstmt.executeUpdate();

Set value John to the first placeholder.


Set value 3 to the second placeholder.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Session 2

JDBC ADVANCED

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Handling SQLExceptions

When JDBC encounters an error during an interaction with a data source,


it throws an instance of SQLException as opposed to Exception. The
SQLException instance contains the following information:
A description of the error.
Retrieve the String object that contains this description by calling the method
SQLException.getMessage.

A SQLState code.
These codes and their respective meanings have been standardized by ISO/ANSI
and Open Group (X/Open), although some codes have been reserved for database
vendors to define for themselves. This String object consists of five alphanumeric
characters. Retrieve this code by calling the method SQLException.getSQLState.

An error code.
This is an integer value identifying the error that caused the SQLException
instance to be thrown. Its value and meaning are implementation-specific and
might be the actual error code returned by the underlying data source. Retrieve
the error by calling the method SQLException.getErrorCode.

A cause.
A SQLException instance might have a causal relationship, which consists of one
or more Throwable objects that caused the SQLException instance to be thrown.
To navigate this chain of causes, recursively call the method
SQLException.getCause until a null value is returned.

A reference to any chained exceptions.


If more than one error occurs, the exceptions are referenced through this chain.
Retrieve these exceptions by calling the method SQLException.getNextException
on the exception that was thrown.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Retrieving Exceptions

The following method,


JDBCTutorialUtilities.printSQLException outputs the
SQLState, error code, error description, and cause (if there
is one) contained in the SQLException as well as any other
exception chained to it:

public static void printSQLException(SQLException ex) {


for (Throwable e : ex) {
if (e instanceof SQLException) {
if (ignoreSQLException(((SQLException)e).getSQLState()) == false) {
e.printStackTrace(System.err);
System.err.println("SQLState: " + ((SQLException)e).getSQLState());
System.err.println("Error Code: " + ((SQLException)e).getErrorCode());
System.err.println("Message: " + e.getMessage());
Throwable t = ex.getCause();
while(t != null) {
System.out.println("Cause: " + t);
t = t.getCause();
}
}
}
}
}

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Retrieving Exceptions

For example, if you execute a SQL


statement but the table does not
exist, the output will be similar to
the following:

SQLState: 42Y55
Error Code: 30000
Message: 'DROP TABLE' cannot be performed on
BOOKS.SAMPLEAUTHORS' because it does not exist.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Batch updates

A set of multiple update statements that is


submitted to the database for processing
as a batch
Statement, PreparedStatement and
CallableStatement can be used to submit
batch updates

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Implementing Batch Update using


Statement interface

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Introduction to Transactions

Concept of Transaction:
One or more statement executed
together
Indivisible unit of work

Properties of Transaction
Atomicity
Consistency
Isolation
Durability

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Transaction

A transaction is a set of one or more


statements that is executed as a
unit, so either all of the statements
are executed, or none of the
statements is executed.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Transaction Processing

Start transaction
Disable auto-commit mode
The way to allow two or more statements to be grouped into a
transaction is to disable the auto-commit mode.
con.setAutoCommit(false);

Perform transaction
Execute queries

Committing transaction
Commit
After the auto-commit mode is disabled, no SQL statements are
committed until you call the method commit() explicitly. All statements
executed after the previous call to the method commit() are included in
the current transaction and committed together as a unit.

Enable auto-commit mode


con.setAutoCommit(true);

Rollback transaction
If any error of abnormal case happened, you may want to aborts this
transaction and restores values to what they were before the
attempted update:
con.rollback()

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Setting and Rolling Back to Savepoints


An SQL savepoint represents the state
of data and schemas at a particular
point in time within a unit of work.
To create a savepoint:

Savepoint savepoint = con.setSavepoint()


Savepoint savepoint = con.setSavepoint(String name)

To remove a savepoint:
con.releaseSavepoint(Savepoint savepoint)

To rollback to a savepoint:
rollback(Savepoint savepoint)

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

RowSet

A JDBC RowSet object holds tabular data in a way that


makes it more flexible and easier to use than a result
set.
All RowSet objects are derived from the ResultSet
interface and therefore share its capabilities. What
makes JDBC RowSet objects special is that they add
these new capabilities:
Function as JavaBeans Component
Add Scrollability or Updatability

Oracle has defined five RowSet interfaces for some of


the more popular uses of a RowSet, and standard
reference are available for these RowSet interfaces:
JdbcRowSet
CachedRowSet
WebRowSet
JoinRowSet
FilteredRowSet

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Function as JavaBeans Component

All RowSet objects are JavaBeans components.


This means that they have the following:
Properties
All RowSet objects have properties. A property is a field that
has corresponding getter and setter methods.

JavaBeans Notification Mechanism


RowSet objects use the JavaBeans event model, in which
registered components are notified when certain events
occur. For all RowSet objects, three events trigger
notifications:
A cursor movement
The update, insertion, or deletion of a row
A change to the entire RowSet contents

The notification of an event goes to all listeners,


components that have implemented the RowSetListener
interface and have had themselves added to the RowSet
object's list of components to be notified when any of the
three events occurs.
FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Setting Up Listeners

A listener for a RowSet object is a component that implements


the following methods from the RowSetListener interface:
cursorMoved: Defines what the listener will do, if anything, when the
cursor in the RowSet object moves.
rowChanged: Defines what the listener will do, if anything, when one or
more column values in a row have changed, a row has been inserted, or
a row has been deleted.
rowSetChanged: Defines what the listener will do, if anything, when the
RowSet object has been populated with new data.

An example of a component that might want to be a listener is


a BarGraph object that graphs the data in a RowSet object. As
the data changes, the BarGraph object can update itself to
reflect the new data.
The following line of code means that every time the cursor for
the crs objects moves, values in crs are changed, or crs as a
whole gets new data, the BarGraph object bar will be notified:
crs.addRowSetListener(bar);
You can also stop notifications by removing a listener:
crs.removeRowSetListener(bar);

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Add Scrollability or Updatability

Some DBMSs do not support result sets that


can be scrolled (scrollable), and some do
not support result sets that can be updated
(updatable). If a driver for that DBMS does
not add the ability to scroll or update result
sets, you can use a RowSet object to do it.
A RowSet object is scrollable and updatable
by default, so by populating a RowSet
object with the contents of a result set, you
can effectively make the result set
scrollable and updatable.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Different types of RowSet

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Kinds of RowSet Objects

A RowSet object is considered either connected or


disconnected.
A connected RowSet object uses a JDBC driver to make a
connection to a relational database and maintains that
connection throughout its life span.
JdbcRowSet

A disconnected RowSet object makes a connection to a data


source only to read in data from a ResultSet object or to write
data back to the data source. After reading data from or
writing data to its data source, the RowSet object
disconnects from it, thus becoming "disconnected." During
much of its life span, a disconnected RowSet object has no
connection to its data source and operates independently.

CachedRowSet
WebRowSet
JoinRowSet
FilteredRowSet

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Stored Procedures

A stored procedure is a group of SQL


statements that form a logical unit and
perform a particular task, and they are
used to encapsulate a set of operations or
queries to execute on a database server.
Stored procedures can be compiled and
executed with different parameters and
results, and they can have any
combination of input, output, and
input/output parameters.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Creating Stored Procedure


public void createProcedureShowAuthors() throws SQLException {
String queryDrop = "DROP PROCEDURE IF EXISTS SHOW_AUTHORS";
String createProcedure =
"create procedure SHOW_AUTHORS() " +
"begin " +
"SELECT firstName, lastName, isbn " +
"FROM authors, authorISBN " +
"INNER JOIN authorISBN " +
"ON authors.authorID = authorISBN.authorID " +
"ORDER BY lastName, firstName; " +
"end";
Statement stmt = null;
Statement stmtDrop = null;
try {
System.out.println("Calling PROCEDURE");
stmtDrop = con.createStatement();
stmtDrop.execute(queryDrop);
stmt = con.createStatement();
stmt.executeUpdate(createProcedure);
} catch (SQLException e) {
System.out.println(e.getMessages());
} finally {
if (stmt != null) { stmt.close(); }
}
}

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Calling Stored Procedures


CallableStatement cs =
this.con.prepareCall("{call SHOW_AUTHORS()}");
ResultSet rs = cs.executeQuery();
while (rs.next()) {
String supplier = rs.getString("firstName");
String coffee = rs.getString("lastName");
...
}

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Connection pool
A connection pool is a cache of
database connections maintained so
that the connections can be reused
when future requests to the
database are required.
Connection pools are used to
enhance the performance of
executing commands on a database.

FPT SOFTWARE TRAINING MATERIAL Internal use

54

04e-BM/NS/HDCV/FSOFT v2/3

Connection pool

Opening and maintaining a database


connection for each user, is costly and
wastes resources.
In connection pooling, after a connection
is created, it is placed in the pool and it is
used over again so that a new connection
does not have to be established.
If all the connections are being used, a
new connection is made and is added to
the pool.

FPT SOFTWARE TRAINING MATERIAL Internal use

55

04e-BM/NS/HDCV/FSOFT v2/3

Connection pool

Client 1

Connection
pool

Databas
e

A connection pool with one connected client

FPT SOFTWARE TRAINING MATERIAL Internal use

56

04e-BM/NS/HDCV/FSOFT v2/3

Connection pool

Client 1
Client 2

Connection
pool

Databas
e

A new client #2 is assigned to a free connection

FPT SOFTWARE TRAINING MATERIAL Internal use

57

04e-BM/NS/HDCV/FSOFT v2/3

Connection pool

Client 2

Connection
pool

Databas
e

Client #1 is disconnected, and free a connection for use

FPT SOFTWARE TRAINING MATERIAL Internal use

58

04e-BM/NS/HDCV/FSOFT v2/3

Connection pool

Client 3
Client 2

Connection
pool

Databas
e

A new client #3 is assigned to the free connection

FPT SOFTWARE TRAINING MATERIAL Internal use

59

04e-BM/NS/HDCV/FSOFT v2/3

How to implement?
You may write your own code
You may using Application server
which supported connection pool (for
ex: Tomcat, Jboss, Jetty, )
You may using other libraries

FPT SOFTWARE TRAINING MATERIAL Internal use

60

04e-BM/NS/HDCV/FSOFT v2/3

Application server

FPT SOFTWARE TRAINING MATERIAL Internal use

61

04e-BM/NS/HDCV/FSOFT v2/3

Declare JNDI sample

In the web.xml:

<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances
</description>
<res-ref-name>
jdbc/EmployeeDB
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>

FPT SOFTWARE TRAINING MATERIAL Internal use

62

04e-BM/NS/HDCV/FSOFT v2/3

Tomcat configuration

The configuration properties for Tomcat's standard


data source resource factory are as follows:
driverClassName - Fully qualified Java class name of the JDBC driver to be
used.
username - Database username to be passed to our JDBC driver.
password - Database password to be passed to our JDBC driver.
url - Connection URL to be passed to our JDBC driver.
initialSize - The initial number of connections that will be created in the pool
during pool initialization. Default: 0
maxActive - The maximum number of connections that can be allocated
from this pool at the same time. Default: 8
minIdle - The minimum number of connections that will sit idle in this pool at
the same time. Default: 0
maxIdle - The maximum number of connections that can sit idle in this pool
at the same time. Default: 8
maxWait - The maximum number of milliseconds that the pool will wait
(when there are no available connections) for a connection to be returned
before throwing an exception. Default: -1 (infinite)

FPT SOFTWARE TRAINING MATERIAL Internal use

63

04e-BM/NS/HDCV/FSOFT v2/3

Tomcat configuration sample


<Context ...>
...
<Resource name="jdbc/EmployeeDB"
auth="Container"
type="javax.sql.DataSource"
username="dbusername"
password="dbpassword"
driverClassName="org.hsql.jdbcDriver"
url="jdbc:HypersonicSQL:database"
maxActive="8"
maxIdle="4"/>
...
</Context>

FPT SOFTWARE TRAINING MATERIAL Internal use

64

04e-BM/NS/HDCV/FSOFT v2/3

Java code sample


Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
// use this connection to access the database ...
conn.close();

FPT SOFTWARE TRAINING MATERIAL Internal use

65

04e-BM/NS/HDCV/FSOFT v2/3

Guide to use HSQL & MySQL

HSQL in-memory
Guide to use HSQL in-memory.doc
SampleHSQL.jar
SampleHSQL.zip

MySQL
Guide to use MySQL.doc
MysqlConnect.java

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Summary

JDBC is an API specification developed by Sun


Microsystems that defines a uniform interface for
accessing different relational databases. The primary
function of the JDBC API is to allow the developer to
issue SQL statements and process the results in a
consistent, database-independent manner.
A JDBC driver translates standard JDBC calls into a
network protocol or client API call that facilitates
communication with the database. This transla-tion
provides JDBC applications with database
independence.
The basic process of connecting to a database via
JDBC goes like this: regis-ter the JDBC driver,
establish a database connection, execute an SQL
state-ment, process the results, close the database
connection.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Summary(cont)

A prepared statement is an SQL statement that is


precompiled by the data-base. Through
precompilation, prepared statements improve the
performance of SQL commands that are executed
multiple times (given that the database supports
prepared statements).
A transaction is a set of SQL statements that are
grouped such that all state-ments are guaranteed to
be executed or the entire operation will fail. If all
statements execute successfully, the results are
committed to the database; otherwise, all changes
are rolled back.
A stored procedure is an SQL operation that is stored
on the database server. Stored procedures are usually
written in an SQL dialect that has been expanded to
include conditional statements, looping constructs,
and other procedural programming features.

FPT SOFTWARE TRAINING MATERIAL Internal use

04e-BM/NS/HDCV/FSOFT v2/3

Q&A

FPT SOFTWARE TRAINING MATERIAL Internal use

69

04e-BM/NS/HDCV/FSOFT v2/3

You might also like