Unit-1 JDBC
Unit-1 JDBC
CONNECTIVITY
• Introduction to JDBC
• JDBC Architecture
• Common JDBC Components
• JDBC Driver
• Java Database Connectivity
Prepared By:
Prof. Jigar Bhawsar
Introduction to JDBC
• stands for Java Database Connectivity
• to connect and execute the query with the database
• uses JDBC drivers to connect with the database
• There are four types of JDBC drivers:
✔ JDBC-ODBC Bridge Driver,
✔ Native Driver,
✔ Network Protocol Driver, and
✔ Thin Driver
Continue…
Continue..
• The java.sql package contains classes and interfaces for
JDBC API
• A list of popular interfaces of JDBC API are given below:
✔ Driver interface
✔ Connection interface A list of popular classes of
✔ Statement interface JDBC API are given below:
✔ PreparedStatement interface ✔ DriverManager class
✔ Blob class
✔ CallableStatement interface
✔ Clob class
✔ ResultSet interface ✔ Types class
✔ ResultSetMetaData interface
✔ DatabaseMetaData interface
✔ RowSet interface
Why Should We Use JDBC
• Before JDBC, ODBC API was the database API to
connect and execute the query with the database
• ODBC API uses ODBC driver which is written in C
language (i.e. platform dependent and unsecured)
• That is why Java has defined its own API (JDBC API)
that uses JDBC drivers (written in Java language)
Continue…
• We can use JDBC API to handle database using Java
program and can perform the following activities:
1. Connect to the database
2. Execute queries and update statements to the database
3. Retrieve the result received from the database.
JDBC Architecture
• JDBC API supports both two-tier and three-tier processing
models
• JDBC Architecture consists of two layers −
✔ JDBC API: This provides the application-to-JDBC Manager
connection.
✔ JDBC Driver API: This supports the JDBC Manager-to-Driver
Connection.
• uses a driver manager and database-specific drivers to
provide transparent connectivity to heterogeneous
databases.
• JDBC driver manager ensures that the correct driver is used
to access each data source.
• The driver manager is capable of supporting multiple
concurrent drivers connected to multiple heterogeneous
databases.
Continue…
Common JDBC Components
• DriverManager: This class manages a list of database
drivers. Matches connection requests from the java
application with the proper database driver using
communication sub protocol. The first driver that
recognizes a certain subprotocol under JDBC will be used
to establish a database Connection.
❖ Disadvantages:
✔ Performance degraded because JDBC method call is converted
into the ODBC function calls.
✔ The ODBC driver needs to be installed on the client machine.
2) Native-API driver
Native API driver uses the client-side libraries of the database The
driver converts JDBC method calls into native calls of the database API.
It is not written entirely in java
❖ Advantage:
✔ performance upgraded than JDBC-ODBC bridge driver.
❖ Disadvantage:
✔ The Native driver needs to be installed on the each client machine.
✔ The Vendor client library needs to be installed on client machine.
3) Network Protocol driver
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.
Continue..
❖ Advantage:
✔ Better performance than all other drivers.
✔ No software is required at client side or server side.
❖ Disadvantage:
✔ Drivers depend on the Database.
Java Database Connectivity
1) Register the driver class
✔ The forName() method of Class class is used to register the
driver class. This method is used to dynamically load the driver
class.
Syntax
public static void forName(String className)throws ClassNotFoun
dException
Example
Class.forName("oracle.jdbc.driver.OracleDriver");
2) Create the connection object
✔ getConnection() method of DriverManager class is used to establish
connection with the database
Syntax
1) public static Connection getConnection(String url)throws
SQLException
2)public static Connection getConnection(String url,String name,
String password) throws SQLException
Example
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
3) Create the Statement object
✔ The createStatement() method of Connection interface is used to
create statement
✔ responsible to execute queries with the database.
Syntax
public Statement createStatement()throws SQLException
Example
Statement stmt=con.createStatement();
4) Execute the query
✔ 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.
Syntax
Syntax
Example
con.close();
Resultset metadata
• 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
ResultSet is useful because it
Metadata
provides methodsInterface
to get metadata from the ResultSet
object
Method Description
• Create
• Retrieve
• Update
• Delete
JDBC transactions
• Transaction represents a single unit of work.
• The ACID properties describes the transaction
management well.
• ACID stands for Atomicity, Consistency, isolation and
durability.
Method Description