Java DB For Begginer 3
Java DB For Begginer 3
to:
Use the JDBC API to access databases
Describe about JDBC drivers
Understand how to use JDBC technology in Java
Applications
know how to load JDBC drivers
retrieve database metadata
call stored procedures
understand scrollable and updatable result set
3.1. Introduction
Programs connect to, and interact with, relational databases via an interface
particular DBMS and allows you to retrieve and manipulate database data.
Package java.sql contains classes and interfaces for accessing relational
databases in Java.
Using the JDBC API enables developers to change the underlying DBMS
2. Creating a connection
3. Creating statement
4. Executing queries
5. Closing a connection
……….Manipulating Database
with JDBC
Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
……….Manipulating Database
with JDBC
queries to the database. This method returns the object of ResultSet that can
be used to get all the records of a table.
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
……….Manipulating Database
with JDBC
Example : con.close();
JDBC example
JDBC CLASSES AND INTERFACES
o DriverManager class
4) public void commit(): saves the changes made since the previous
commit/rollback permanent.
5) public void rollback(): Drops all changes made since the previous
commit/rollback.
ResultSet static
Description
type constant
TYPE_FORWARD_ONLY Specifies that a ResultSet’s cursor can move only in the forward
direction (i.e., from the first row to the last row in the ResultSet).
TYPE_SCROLL_INSENSITIVE Specifies that a ResultSet’s cursor can scroll in either direction
and that the changes made to the ResultSet during ResultSet
processing are not reflected in the ResultSet unless the program
queries the database again.
TYPE_SCROLL_SENSITIVE Specifies that a ResultSet’s cursor can scroll in either direction
and that the changes made to the ResultSet during ResultSet
processing are reflected immediately in the ResultSet.
ResultSet static
Description
concurrency constant
CONCUR_READ_ONLY Specifies that a ResultSet cannot be updated (i.e., changes to the ResultSet
contents cannot be reflected in the database with ResultSet’s update methods).
CONCUR_UPDATABLE Specifies that a ResultSet can be updated (i.e., changes to the ResultSet
contents can be reflected in the database with ResultSet’s update methods).
Example1
..........JDBC CLASSES AND
INTERFACES
o ResultSetMetaData Interface
The metadata means data about data i.e. we can get further information from
the data. If you have to get metadata of a table like total number of column,
column name, column type etc. , ResultSetMetaData interface is useful
because it provides methods to get metadata from the ResultSet object.
Method Description
public int it returns the total number of
getColumnCount()throws columns in the ResultSet
SQLException object.
public String
it returns the column name of
getColumnName(int
the specified column index.
index)throws SQLException
public String
it returns the column type
getColumnTypeName(int
name for the specified index.
index)throws SQLException
public String getTableName(int it returns the table name for
index)throws SQLException the specified column index.
Example1 Example2
..........JDBC CLASSES AND
INTERFACES
o DatabaseMetaData interface
Syntax:
The ACID properties describes the transaction management well. ACID stands
for Atomicity, Consistency, isolation and durability.
Atomicity means either all successful or none.
Durability means once a transaction has been committed, it will remain so,
manage transaction.
Method Description
void It is true bydefault means
setAutoCommit(boolean each transaction is
status) committed bydefault.
void commit() commits the transaction.
void rollback() cancels the transaction.
Example
…… Transaction Management in
JDBC
o Batch Processing in JDBC
2. Create Connection
3. Create Statement
5. Execute Batch
6. Close Connection
Example
…… Transaction Management in
JDBC
o JDBC RowSet
The instance of RowSet is the java bean component because it has properties
and java bean notification mechanism. It is the wrapper of ResultSet. It holds
tabular data like ResultSet but it is easy and flexible to use.
CachedRowSet
WebRowSet
JoinRowSet
FilteredRowSet
…… Transaction Management in
JDBC
o Steps to create and execute RowSet.
o Advantage of RowSet
It is easy and flexible to use
It is Scrollable and Updatable by default
Example
Th
an
k
Yo
u!
!