Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

JDBC

Sabyasachi Moitra
moitrasabyasachi@hotmail.com
What is JDBC?
O Java Database Connectivity.
O Java API
- Application Programming Interface written in Java.
- A set of subroutine definitions, protocols, and tools for
building application software.
O Connects and executes query with the database.
O JDBC API uses JDBC drivers to connect with the database.

2
Block Diagram

Application
Program JDBC API JDBC Driver Database
(.java)

3
What are JDBC Drivers?
O A software component that enables java application to
interact with the database.
O There are 4 types of JDBC drivers:
- JDBC-ODBC Bridge Driver (Type 1)
- Native-API Driver (Type 2)
- Network Protocol Driver (Type 3)
- Thin Driver (Type 4)

4
JDBC-ODBC Bridge Driver
O Uses ODBC driver to connect to the database.
O Converts JDBC method calls into the ODBC function calls.
Advantages
O Easy to use.
O Can be easily connected to any database.
Disadvantages
O Performance degraded because JDBC method call is
converted into the ODBC function calls.
O The ODBC driver needs to be installed on the client machine.
5
Block Diagram

DBMS
JDBC ODBC specific
calls calls calls

Application Vendor
JDBC ODBC ODBC
Program JDBC API Database Database
Driver Driver
(.java) Library

Client Machine

6
Native-API Driver
O Uses the client-side libraries of the database.
O Converts JDBC method calls into native calls of the database API.
O It is not written entirely in java.
Advantage
O Performance upgraded than JDBC-ODBC bridge driver.
Disadvantages
O The Native driver needs to be installed on the each client
machine.
O The Vendor client library needs to be installed on client machine.
7
Block Diagram

DBMS
specific
JDBC calls
calls

Application Vendor
Native API
Program JDBC API Database Database
Driver
(.java) Library

Client Machine

8
Network Protocol Driver
O Uses middleware (application server) that converts JDBC calls directly or
indirectly into the vendor-specific database protocol.
O It is fully written in java.
O Middleware might use Type 1, 2, or 4 drivers to communicate with the
database.
Advantage
O No client side library is required because of application server that can
perform many tasks like auditing, load balancing, logging etc.
Disadvantages
O Network support is required on client machine.
O Requires database-specific coding to be done in the middle tier.
O Maintenance of Network Protocol driver becomes costly.

9
Block Diagram

Middleware
Server DBMS
JDBC specific specific
calls calls calls

Application Network
Program JDBC API Protocol Middleware Database
(.java) Driver

Client Machine Server Side

10
Thin Driver
O Converts JDBC calls directly into the vendor-specific
database protocol.
O It is fully written in Java language.
Advantages
O Better performance than all other drivers.
O No software is required at client side or server side.
Disadvantage
O Drivers depends on the Database.

11
Block Diagram

DBMS
JDBC specific
calls calls

Application
Program JDBC API Thin Driver Database
(.java)

Client Machine

12
Database Connectivity Steps
Register the
driver class

Creating
connection

Creating
statement

Executing
queries

Closing
connection

13
Java Oracle Connectivity

14
Java MySQL Connectivity

15
Output

16
DriverManager Class
O Acts as an interface between user and drivers.
O Keeps track of the drivers that are available.
O Handles establishing a connection between a database and
the appropriate driver.
O Maintains a list of Driver classes that have registered
themselves.

17
Methods of DriverManager Class
Method Description
public static void registerDriver(Driver Used to register the given driver with
driver) DriverManager.
public static void deregisterDriver(Driver Used to deregister the given driver (drop
driver) the driver from the list) with
DriverManager.
public static Connection Used to establish the connection with
getConnection(String url) the specified url.
public static Connection Used to establish the connection with
getConnection(String url,String the specified url, username and
userName,String password) password.

18
Connection Interface
O A Connection is the session between java application and
database.
O The Connection interface is a factory of Statement,
PreparedStatement, and DatabaseMetaData, i.e., the object
of Connection can be used to get the object of Statement
and DatabaseMetaData.

19
Methods of Connection Interface
Method Description
public Statement createStatement() Creates a statement object that can be
used to execute SQL queries.
public void commit() Saves the changes made since the
previous commit/rollback permanently.
public void rollback() Drops all changes made since the
previous commit/rollback.
public void close() Closes the connection and releases a
JDBC resources immediately.
…..

20
Statement Interface
O Provides methods to execute queries with the database.
O The Statement interface is a factory of ResultSet, i.e., the
object of Statement can be used to get the object of
ResultSet.

21
Methods of Statement Interface
Method Description
public ResultSet executeQuery(String • Used to execute SELECT query.
sql) • Returns the object of ResultSet.
public int executeUpdate(String sql) Used to execute specified query, it may
be create, drop, insert, update, delete
etc.
…..

22
ResultSet Interface
O The object of ResultSet maintains a cursor pointing to a row
of a table.
O Initially, cursor points to before the first row.

23
Methods of ResultSet Interface
Method Description
public int getInt(String columnName) Used to return the data of specified
column name of the current row as int.
public String getString(String Used to return the data of specified
columnName) column name of the current row as
String.
…..

24
PreparedStatement Interface
O The PreparedStatement interface is a sub interface of
Statement.
O It is used to execute parameterized query.
String sql="insert into course (cid,cname) values(?,?)";
• We are passing parameter (?) for the values.
• Its value will be set by calling the setter methods of PreparedStatement.

O Improves the performance of the application as the query is


compiled only once.
O Dynamic SQL in terms of Java.

25
Methods of PreparedStatement Interface
Method Description
public void setInt(int paramIndex, int value) Sets the integer value to the given parameter
index.
public void setString(int paramIndex, String Sets the String value to the given parameter
value) index.
public void setFloat(int paramIndex, float value) Sets the float value to the given parameter
index.
public void setDouble(int paramIndex, double Sets the double value to the given parameter
value) index.
public int executeUpdate() • Executes the query.
• It is used for create, drop, insert, update,
delete etc.
public ResultSet executeQuery() • Executes the select query.
• It returns an instance of ResultSet. 26
INSERT INTO Statement

27
UPDATE Statement

28
DELETE Statement

29
CallableStatement Interface
O CallableStatement interface is used to call the stored
procedures and functions.

30
PROCEDURE vs FUNCTION
Procedure Function
Must not have the return type. Must have the return type.
May return 0 or more values. May return only one values.
We can call functions from the Procedure cannot be called from
procedure. function.
Procedure supports input and output Function supports only input parameter.
parameters.
Three types of arguments:-
IN  Input type
OUT  Output type
INOUT  Both type
31
Example
Procedure Function
create or replace procedure proc_sum create or replace function sum4
(n1 in number,n2 in number,temp out (n1 in number,n2 in number)
number) return number
is is
begin temp number(8);
temp :=n1+n2; begin
end; temp :=n1+n2;
/ return temp;
end;
/

32
Calling Procedure

33
Calling Function

34
References
O Courtesy of JavaTPoint – Java JDBC Tutorial. URL:
http://www.javatpoint.com/java-jdbc
O Courtesy of TutorialsPoint – JDBC Tutorial. URL:
https://www.tutorialspoint.com/jdbc/

35

You might also like