JDBC : Java Database Connectivity

723 views 22 slides May 10, 2021
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

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 dat...


Slide Content

JDBC
Dev Adnani IU1983830001
JAVA DATABASE CONN.

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

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.

OTHER
MySQL
ORACLE
JDBC API
Java
Application
JDBC Architecture
JDBC
Driver
Manager
Driver
Driver
Driver

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.

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!!

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:

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:

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:

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:

Configuration Properties For JDBC connection
Driver Manager
Connection
Prepared Statement
Result Set
SQLException

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.

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.

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

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.

Step for JDBC connection
Loading driver
Establishing connection
Preparing statement
Executing statement
Getting result
Close DB

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());

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”

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();

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");

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();

Thank You
Any Questions?
ADNANI DEV
IU1983830001