You are on page 1of 26

Presentation by:

JOSHI
JDBC
Java Data Base Connectivity
ODBC
It is dependent on O.S. as it exist in the form of .dll

connection
DRIVER 1 DB
Request connection
CLIENT DRIVER 2 DB
Application
connection
DRIVER 3 DB
ODBC Driver
Manager SERVER
JDBC
It is independent of O.S. which is specific to JRE.

JDBC is an API with set of classes and interfaces


present in package java.sql

JAVA
JDBC JRE DRIVER
APP DB

CLIENT SERVER
JDBC Architecture
JDBC API Application

JDBC Driver Manager


JDBC.ODBC
JDBC JDBC BRIDGE
Driver A Driver B
ODBC

DB DB DB
JDBC
JDBC supports 4 types of Drivers

• JDBC-ODBC Bridge Driver.


• Native-API Partly Java Driver
• Net-Protocol Fully Java Driver
• Native-Protocol Fully Java Driver
JDBC-ODBC Bridge Driver
•A JDBC-ODBC bridge provides JDBC API
access via one or more ODBC drivers.
•some ODBC native code and in many cases
native database client code must be loaded
on each client machine that uses this type of
driver.
•It is appropriate when automatic installation
and downloading of a Java technology
application is not important.
JDBC-ODBC Bridge Driver

JDBC-ODBC BRIDGE

J O
JAVA D D
APP DB
B B
C C SERVER
CLIENT
Native-API Partly Java Driver

•This driver converts JDBC calls into


calls on the client API for Oracle,
SQLServer, Sybase, Informix, DB2, or
other DBMS.

•Like the bridge driver, this style of


driver requires that some binary code
be loaded on each client machine.
Native-API Partly Java Driver

J O
OCI RACL
JAVA D E
App
B DB
SQL
Library
C Server

CLIENT SERVER
Net-Protocol Fully Java Driver
•This driver translates JDBC API calls into a
DBMS-independent net protocol which is then
translated to a DBMS protocol by a server.

•This net server middleware is able to connect


all of its Java technology-based clients to many
different databases.

•The specific protocol used depends on the


vendor. If vender changes then application will
not work.
Net-Protocol Fully Java Driver

JAVA JDBC Proto ODBC DB


col
1 Middle
App Driver 1
ware OCI O
Appli racl
e
JAVA JDBC o l 2 cation
r o toc DB
App Driver 2 P SQL
Library Ser

APPLICATION
CLIENT SERVER SERVER
Native-Protocol Fully Java Driver
• This driver converts JDBC calls into the
network protocol used by DBMSs directly.
• This allows a direct call from the client to
the DBMS server and is a practical solution
for Intranet access.
• As protocols are proprietary to database
vendors, they will be the primary source
for this style of driver.
• Several database vendors have these in
progress.
Native-Protocol Fully Java Driver

JAVA JDBC Data


App Driver Base
PROTOCOL

user Sockets & Streams


Loading DriverManager
Syntax of loading the Class file

Class.forName(“DriverManagerClass”);

Example for Jdbc-Odbc bridge driver:

Class.forName(“sun.jdbc.odbc
.JdbcOdbcDriver”);
Connecting Through DSN
Connection con =
DriverManager.getConnection(“jdbc:odbc
:DSNName”);
Or

DriverManager.getConnection(“jdbc:odbc
:DSNName”, “username”, “password”);
Data Source Name
• User DSN
– Available for user who creates and
stores in registry
• System DSN
– Available for all users and stores in
registry
• File DSN
– It stores in File
Statements
1.Statement
It is used to execute SQL statements

2.Prepared Statement
Used to prepare statements with place
holders(?) to set the values at run time

3.Callable Statement
Used to execute functions or procedures
available in data base
Statement
Statement smt = con.createStatement();

ResultSet rs =
smt.executeQuery(“Select_Queries”);

int n =
smt.executeUpdate(“DML_Queries”);

boolean b =
smt.execute(“Other_Queries”);
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.
ResultSet
• Navigating from one record to another
boolean b = rs.next()

• Extracting values from ResultSet is possible


either by Field Name or Field Index

int n=rs.getInt(int/String);
String s=rs.getString(int/String);
….
XXX x=rs.getXXX(int/String);
ResultSetMetaData
• It is Data about Data of ResultSet like field
names, no. of Columns etc.
ResultSetMetaData rsmd = rs.getMetaData();

int count = rsmd.getColumnCount();


Strting fname = rsmd.getColumnName(int index);
String alias = rsmd.getColumnLabel(int index);
int dn = rsmd.getColumnType(int index);
String dn = rsmd.getColumnTypeName(int index);
PreparedStatement
PreparedStatement ps =
con.prepareStatement(“Query with Place Holders”);

Example:
PreparedStatement ps =
con.prepareStatement(“select * from emp where
empno=?”);

PreparedStatement ps =
con.prepareStatement(“insert into emp(empno,
ename, sal, deptno) values(?,?,?,?)”);
PreparedStatement
Set values for place holders
ps.setInt(int, int);
ps.setString(int, String);
……..
ps.setXXX(int, XXX);

Executing the PreparedStatement


ResultSet rs=ps.executeQuery();
int n=ps.executeUpdate();
boolean b=ps.execute();
CallableStatement
• CallableStatement cs =
con.prepareCall(“{call fun/prod(?,?)}”);

Working with OUT/INOUT parameter


cs.registerOutparameter(int ind,int Types)

• Example:
• CallableStatement cs =
con.prepareCall(“{call emp.insert(?,?,?)}”);
CallableStatement
• Set values for place holders
ps.setInt(int, int);
ps.setString(int, String);
ps.setXXX(int, XXX);
• Executing the PreparedStatement
ResultSet rs=cs.getResultSet();
boolean b=cs.execute();

You might also like