JDBC is an API that provides methods for connecting to a database and executing SQL statements. There are 4 types of JDBC drivers: Type 1 uses ODBC, Type 2 uses the native API, Type 3 is fully Java and uses network protocols, and Type 4 is fully Java and uses the native database protocol. The basic steps for using JDBC are: loading the driver class, getting a connection, executing statements using objects like Statement, PreparedStatement, and CallableStatement, and closing the connection.
2. JDBC
JDBC is an API with set of classes and interfaces
present in package java.sql
Thick Client
JAVA
APP
Thin
Client
JAVA
APP
DRIVER
C LIENT
DRIVER
C LIENT
Supporting Software
DB
SERVER
DB
SERVER
4. JDBC-ODBC Bridge Driver (TYPE – I)
JDBC API access via one or more ODBC drivers.
Binary code be loaded on each client machine
JDBC-ODBC BR
IDGE
JAVA
APP
J
D
B
C
O
D
B
C
DB
SERVER
C LIENT
5. ODBC
The Open Database Connectivity (ODBC) interface by Microsoft
allows applications to access data in database management
systems (DBMS) using SQL as a standard for accessing the data.
DRIVER 1
CLIENT
A pplication
Request
connection
DRIVER 2
connection
DRIVER 3
connection
ODBC Driver
Manager
DB
DB
DB
SERVER
6. Native-API Partly Java Driver (TYPE – II)
Binary code should be loaded on each client machine
Converts JDBC calls into calls on the client API for all
databases
J
JAVA
App
D
B
C
C LIENT
OCI
DB Library
ORACLE
SQL
Server
SERVER
7. Net-Protocol Fully Java Driver (TYPE – III)
The specific protocol used depends on the vendor
Type 1
JA VA
App
Type 3
Dr iver
Protocol 1
Middle
ware
Appli
cation
DB1
Type 2
DB2
Type 4
CLIENT
APPLICATION
SERVER
SERVER
DB3
8. Native-Protocol Fully Java Driver(TYPE – IV)
Direct call from the client to the DBMS server and is a
practical solution for Intranet access.
protocols are proprietary to database vendors, primary
source for this style of driver.
JAVA
A pp
JDBC
Driver
Data Base
PROTOCOL
User Sockets & Streams
9. JDBC
The 4 steps to connect to database are:
Loading the Driver.
Getting the Connection to database.
Executing the SQL Query.
Closing the Connection.
10. Loading Driver
Syntax for loading the Class file
Class.forName(“Driver Class Name”);
Example for Jdbc-Odbc bridge driver:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Class.forName("oracle.jdbc.driver.OracleDriver");
12. Data Source Name (DSN)
User DSN
Available for user who creates and
stores in registry
System DSN
Available for all users and stores in
registry
13. Various ways of getting database connection
Connection con =
DriverManager.getConnection(“jdbc:odbc:dsnname”);
DriverManager.getConnection(“jdbc:odbc:dsnname”,
“username”, “password”);
DriverManager.getConnection(“jdbc:oracle:oci”, “username”,
“password”);
DriverManager.getConnection(“jdbc:oracle:thin:@ipaddress:port
:serviceId”, “username”, “password”)
14. Statement
Statement
It is used to execute SQL statements
Prepared Statement
Used to prepare statements with place holders(?)
to set the values at run time
Callable Statement
Used to execute functions or procedures available
in data base
15. Statement
A Statement object is used for executing a static SQL
statement and obtaining the results produced by it.
Statement smt = con.createStatement();
R
esultSet rs = smt.executeQuery(“Select_Queries”);
int n = smt.executeUpdate(“DML_Queries”);
boolean b = smt.execute(“Any_Query”);
16. ResultSet
Is an Object which stores data of the select statement
result in records and fields form.
By default it is Forward Only and Read Only.
Result Set Navigation and updating is possible from new
API version 1.2 onwards.
17. ResultSet
Navigating from one record to another
boolean b = rs.next()
Extracting values from ResultSet is possible either by Field Name or
Field Index
Column Index
int n=rs.getInt(1);
or
int n = rs.get( “empID”);
String s=rs.getString(1);
or
String s=rs.getString( “empName”);
Column Name
18. ResultSetMetaData
It is Data about Data of ResultSet like field names, no.
of Columns etc.
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String fname = rsmd.getColumnName(int index);
String dn = rsmd.getColumnTypeName(int index);
19. PreparedStatement
A PreparedStatement object is used when an application plans to
reuse a statement multiple times.
The application prepares the SQL Statement it plans to use. Once
prepared, the application can specify values for parameters (if any)
in the prepared SQL statement.
PreparedStatement ps = con.prepareStatement(“Query with Place
Holders”);
Examples:
PreparedStatement ps = con.prepareStatement(“select * from emp
where empno=?”);
Place Holder
20. PreparedStatement
PreparedStatement ps = con.prepareStatement(“insert into emp
(empno,ename) values(?,?)”);
ps.setInt(1,102);
ps.setString(2, “Scott”);
Executing the PreparedStatement
ResultSet rs=ps.executeQuery();
int n=ps.executeUpdate();
boolean b=ps.execute();
Set values for
place holders
21. Statement vs PreparedStatement
A Statement object
Doesn’t contain a SQL statement at the time of creation.
Compiled and executed every time
A PreparedStatement object
Is a precompiled Statement
Contains a SQL statement at the time of creation.
Support for Place holders, (Ex: For data type blob, clob,
binaryStream)
22. Function
create or replace function DemoFunction (id number)
return varchar2 is
temp varchar2(20);
begin
select name into temp from Student where
rollno=id;
return temp;
end DemoFunction;
23. Procedure
create or replace procedure DemoProcedure(sname
varchar2, rno number) is
begin
insert into Student values(sname,rno);
end DemoProcedure;
24. CallableStatement
A CallableStatement is used to call stored procedures that return
values. The CallableStatement has methods for retrieving the return
values of the stored procedure.
CallableStatement cs = con.prepareCall( “{call prod(?,?)}”);
= con.prepareCall( "{?=call fun(?)}");
Example:
CallableStatement cs = con.prepareCall( “{call emp.insert(?,?)}”);
25. CallableStatement
CallableStatement cs = con.prepareCall(“{?=call empSal(?)}”);
cs.registerOutParameter(1, java.sql.Types.INTEGER);
cs.setInt(2,102);
cs.executeUpdate();
int result = cs.getInt(1);
27. Example Program
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
"scott", "tiger");
CallableStatement cs = con.prepareCall("{?=call empSal(?)}");
cs.setInt(2, 7369);
cs.registerOutParameter(1,Types.INTEGER);
cs.executeUpdate();
int x=cs.getInt(1);
System.out.println(" The Value is: "+ x);
con.close();