This document discusses JDBC (Java Database Connectivity), which provides an API for connecting Java applications to databases. It describes the JDBC architecture and driver types, including JDBC-ODBC bridge drivers, native-API drivers, network protocol drivers, and thin drivers. The document also outlines the steps for establishing a JDBC database connection, including loading the driver, creating a connection, preparing statements, executing queries, retrieving results, and closing the connection.
2. Contents of the
Presentation
What Is JDBC
JDBC Architecture
What Is JDBC Driver
Types of JDBC Drivers
Configuration for JDBC connection
Configuration Properties For JDBC connection
Step for connection JDBC connection
3. What Is JDBC ?
JDBC == Java Database Connectivity
JDBC is used to connect java application with database.
JDBC is an API used to communicate Java application to database in database independent
and platform independent manner.
It provides classes and interfaces to connect or communicate Java application with database.
5. JDBC Driver
JDBC DRIVER is software component enabling a java
application to interact with a database.
It handles the communication with the database
server(oracle/mySQL).
we direct interect with the driver very rerely.
6. JDBC - ODBC (Bridge Driver)
Native - API Driver ( Partially java Driver)
Network Protocol Driver( Fully java Driver)
Thin Driver (Fully java Driver )
Type of JDBC driver!!
7. JDBC - ODBC (BRIDGE DRIVER)
Easy to use.
Can be easily connected to any database.
Performance degraded because JDBC method call is converted into the ODBC
function calls.
The ODBC driver needs to be installed on the client machine.
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.
Advantages:
Disadvantages:
8. NATIVE-API DRIVER
Performance upgraded than JDBC-ODBC bridge driver
The Native driver needs to be installed on the each client machine.
The Vendor client library needs to be installed on client machine
The Native API driver uses the client-side libraries of the database.
The driver converts JDBC method calls into native calls of the database API.
Advantage:
Disadvantage:
9. NETWORK PROTOCOL DRIVER
No client side library is required because of application server that can perform many tasks like
auditing, load balancing, logging etc.
Requires database-specific coding to be done in the middle tier.
Maintenance of Network Protocol driver becomes costly because it requires database-specific
coding to be done in the middle tier
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.
Advantage:
Disadvantages:
10. THIN DRIVER
Better performance.
No software is required .
Drivers depends on the Database.
The thin driver converts JDBC calls into the vendor-specific database protocol.
It is fully written in Java language.
Advantage:
Disadvantage:
11. Configuration Properties For JDBC connection
Driver Manager
Connection
Prepared Statement
Result Set
SQLException
12. DRIVER MANAGER
It matches connection requests from the java
application with the proper database driver using
communication sub-protocol.
The first driver that recognize certain sub-
protocol under JDBC will be used to establish a
database connection.
13. CONNECTION
Connection interface has all methods for contacting a database
The connection object represent communication context.
All communication with database is through connection object only.
14. PREPARED STATEMENT
Prepared Statement is an interface for representing SQL statement.
SQL statement is Precompiled and stored in a Prepared Statement object
Object can then be used efficiently execute this statement multiple time
15. RESULT SET
ResultSet - RS is an interface to represent a database table
These objects holds data retrieved from a database after you execute SQL query using
statement object.
It acts as an iterator to allow you to move through its data.
16. Step for JDBC connection
Loading driver
Establishing connection
Preparing statement
Executing statement
Getting result
Close DB
17. 1. LOADING THE DRIVER
Class.forName() : Here we load the driver’s class file into memory at the
runtime. No need of using new or creation of object .The following example
uses Class.forName() to load the Oracle driver –
DriverManager.registerDriver(): DriverManager is a Java inbuilt class with a
static member register. Here we call the constructor of the driver class at
compile time . The following example uses DriverManager.registerDriver()to
register the Oracle driver –
- Class.forName(“oracle.jdbc.driver.OracleDriver”);
- DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
18. 2. CREATE THE CONNECTIONS
Connection con = DriverManager.getConnection(url,user,password)
user – username from which your sql command prompt can be accessed.
password – password from which your sql command prompt can be accessed.
url : Uniform Resource Locator. It can be created as follows:
String url = “ jdbc:oracle:thin:@localhost:1521:xe”
19. 3. CREATE A STATEMENT
Once a connection is established you can interact with the database. The
JDBCStatement, CallableStatement, and PreparedStatement interfaces define
the methods that enable you to send SQL commands and receive data from
your database.
Statement st = con.createStatement();
20. 4. EXECUTE THE QUERY
Query for updating / inserting table in a database.
Query for retrieving data .
The executeQuery() method of Statement interface is used to execute queries
of retrieving values from the database. This method returns the object of
ResultSet that can be used to get all the records of a table.
ResultSet rset = null;
rset = st.executeQuery("select last_name, first_name from person");
21. 5.CLOSE THE CONNECTIONS
we have sent the data to the specified location and now we are at the verge of
completion of our task .
By closing connection, objects of Statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close the
connection.
con.close();