Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

JDBC Objects

Chapter 6
J2EE Complete Reference
Jim Keogh

Agenda

JDBC Concept
JDBC Driver Types
JDBC Packages
Brief Overview of JDBC process
Database Connection
Connection Pool

Connect to Database
Loading the JDBC
Driver
Connecting to the Database

Accessing the database


Create and execute a SQL Statement

Process Data
Process Data returned by DBMS

Terminate the connection


The easiest
statement :

Database Connection
(more than one way)
Using URL:

Database Connection
(more than one way)
Using user ID and password

Connection Pool
Connecting to database using
Connection Pool

Statement Objects
Contains methods:
1. executeQuery()
- Returns ResultSet objects that contains
rows, columns, metadata.
2. execute()
- Used when there may be multiple results
returned
3. executeUpdate()
- Used to execute DELETE and UPDATE queries
- Returns the number of rows affected.

PreparedStatement
SQL Query must be compiled before DBMS
processes the query.
Compiling occurs after one of the Statement class
execution method is called.
Compiling a query is an overhead.
Acceptable if query is called once.
Expensive if the query is called several times.
A SQL Query is precompiled using
PreparedStatement.

CallableStatement
Uses 3 types of parameters when
calling a stored procedure:
IN : Contains data that needs to be
passed to the stored procedure
OUT: Contains value that is returned by
stored procedure.
INOUT: Single parameter that is used to
pass and store parameter to and from
stored procedure.

ResultSet

SrollableResultSet

first()- Virtual cursor moved to first row


last()-Virtual cursor moved to last row
previous()-Virtual cursor moved to previous row
absolute()-Positions the cursor at the row number
specified as a parameter to the method
relative()- moves the cursor to specified number of
rows. The parameter can be positive or negative.
getRow()- gets the integer that represents the
number of the current row

Usage

Implementation

Updatable ResultSet

Delete Row in ResultSet

Insert Row

Metadata
Metadata is data about data
J2EE component retrieves metadata
about the database by calling
getMetaData() of Connection object.
This method returns
DatabaseMetaData object containing
information about database and its
components.

Retrieving information
DatabaseMetaData object has the following functions
to retrieve the information contained in the object.

You might also like