Documente Academic
Documente Profesional
Documente Cultură
Chapter 3
JDBC
• Introduction
• JDBC Driver
• JDBC Architecture
• JDBC Classes and
Interfaces
• Manipulating Database
with JDBC
• Transaction Management in
JDBC
Programs connect to, and interact with, relational databases via an
Java programs communicate with databases and manipulate their data using
particular DBMS and allows you to retrieve and manipulate database data.
databases in Java.
Using the JDBC API enables developers to change the underlying DBMS
3
o JDBC Driver is a software component that enables java application to
4
1) JDBC-ODBC bridge driver(Type 1)
The JDBC-ODBC bridge driver uses ODBC driver to connect to the
database. The JDBC-ODBC bridge driver converts JDBC method calls into
the ODBC function calls. This is now discouraged because of thin driver.
5
1) JDBC-ODBC
Advantages:
easy to use.
Disadvantages:
7
2) Native-API driver
Advantage:
Disadvantage:
machine.
machine.
8
3) Network Protocol driver (Type 3)
The Network Protocol driver uses middleware (application server) that converts JDBC
calls directly or indirectly into the vendor-specific database protocol. It is fully written
in java.
9
Network Protocol driver
Advantage:
Disadvantages:
10
4) Thin driver –Pure Java (Type 4)
o The thin driver converts JDBC calls directly into the vendor-specific database
protocol. That is why it is known as thin driver. It is fully written in Java language.
11
4) Thin driver
Advantage:
Disadvantage:
12
The JDBC architecture is sometimes classified as: two-tier and
three-tier. Type 2 and 4 drivers use two-tier and Type 1 and 3 use
three-tier architecture.
Figure (i) and (ii) show the JDBC two-tier and three-tier
architectures.
13
Fig. JDBC Architecture (i) Two-tier (ii) Three-tier
14
Java provides an API for accessing and processing data stored in a
data source (usually a relational database).
Table 1: Java JDBC classes and interfaces
15
Steps to connect to the database in java
2. Creating connection
3. Creating statement
4. Executing queries
5. Closing connection
16
Steps to connect to the database in java
Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
17
2) Create the connection object
o The getConnection() method of DriverManager class is used to establish
Connection con=DriverManager.getConnection("jdbc:mysql://hostname:port/dbName",“un",“PW");
18
3) Create the Statement object
o The createStatement() method of Connection interface is used to create
19
4) Execute the query
o The 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)); }
20
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
21
o DriverManager class
22
o Connection interface
23
o Connection interface
Commonly used methods of Connection interface:
24
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.
25
o Statement interface
26
o ResultSet interface
The object of ResultSet maintains a cursor pointing to a particular row of data. Initially,
cursor points to before the first row.
By default, ResultSet object can be moved forward only and it is not updatable.
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:
ResultSet.CONCUR_UPDATABLE);
27
o ResultSet interface
The object of ResultSet maintains a cursor pointing to a particular row of data. Initially,
cursor points to before the first row.
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:
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Example
28
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.
29
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).
30
o PreparedStatement interface
Example1
31
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.
32
o ResultSetMetaData Interface
Method Description
public int getColumnCount()throws it returns the total number of columns in
SQLException the ResultSet object.
public String getColumnName(int it returns the column name of the specified
index)throws SQLException column index.
public String getColumnTypeName(int it returns the column type name for the
index)throws SQLException specified index.
public String getTableName(int it returns the table name for the specified
index)throws SQLException column index.
Example1 Example2
33
o DatabaseMetaData interface
34
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.
Example
35
o CallableStatement Interface
36
How to get the instance of CallableStatement?
Syntax:
37
Example of CallableStatement
To call the stored procedure, you need to create it in the database. Here, we are
assuming that stored procedure looks like this.
38
o Transaction represents a single unit of work.
The ACID properties describes the transaction management well. ACID stands
for Atomicity, Consistency, isolation and durability.
Durability means once a transaction has been committed, it will remain so,
39
o Advantage of Transaction Management
fast performance : it makes the performance fast because database is hit at the time
of commit.
40
o In JDBC, Connection interface provides methods to manage transaction.
Method Description
Example
41
o Batch Processing in JDBC
Instead of executing a single query, we can execute a batch (group) of queries. It makes
the performance fast.
42
Let's see the simple example of batch processing in jdbc.
2. Create Connection
3. Create Statement
5. Execute Batch
6. Close Connection
Example
43
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.
JdbcRowSet
CachedRowSet
WebRowSet
JoinRowSet
FilteredRowSet
44
o Steps to create and execute RowSet.
o Advantage of RowSet
It is easy and flexible to use
It is Scrollable and Updatable bydefault
Example
45