Chapter 3 - JDBC
Chapter 3 - JDBC
Chapter 3 - JDBC
2
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
4
The JDBC architecture is sometimes classified
5
Fig 4.2. JDBC Architecture (i) Two-tier (ii) Three-tier
6
Java provides an API for accessing and
processing data stored in a data source.
Table 1: Java JDBC classes and
interfaces
7
Steps to connect to the database in java
2.Creating a connection
3.Creating statement
4.Executing queries
5.Closing a connection
8
Steps to connect to the database in java
Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
9
2) Create the connection object
10
3) Create the Statement object
oThe createStatement() method of Connection interface is used to create
11
4) Execute the query
oThe executeQuery() method of Statement interface is used to execute 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)); }
12
5) Close the connection object
By closing connection object statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close the
connection.
Example : con.close();
JDBC example
13
o DriverManager class
15
o Connection interface
16
3) public void setAutoCommit(boolean status): is used to set the
commit status. By default it is true.
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.
17
o Statement interface
But we can make this object to move forward and backward direction by
passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in
createStatement(int,int) method as well as we can make this object as
updatable by:
19
o ResultSet interface
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.
20
o ResultSet interface
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).
21
o PreparedStatement interface
Example1
22
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.
23
o ResultSetMetaData Interface
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
24
o DatabaseMetaData interface
25
o methods of DatabaseMetaData interface
public String getDriverName()throws SQLException: it returns
the name of the JDBC driver.
public String getDriverVersion()throws SQLException: it
returns the version number of the JDBC driver.
public String getUserName()throws SQLException: it returns
the username of the database.
public String getDatabaseProductName()throws
SQLException: it returns the product name of the database.
public String getDatabaseProductVersion()throws
SQLException: it returns the product version of the database.
public ResultSet getTables(String catalog, String
schemaPattern, String tableNamePattern, String[]
types)throws SQLException: it returns the description of the
tables of the specified catalog. The table type can be TABLE, VIEW,
ALIAS, SYSTEM TABLE, SYNONYM etc.
26
Example
o CallableStatement Interface
27
How to get the instance of CallableStatement?
Syntax:
28
Example of CallableStatement
Durability means once a transaction has been committed, it will remain so,
30
o Advantage of Transaction Management
31
o In JDBC, Connection interface provides methods to
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
32
o Batch Processing in JDBC
2.Create Connection
3.Create Statement
5.Execute Batch
6.Close Connection
Example
34
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
35
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
36
37