Professional Documents
Culture Documents
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.
JAVA
JDBC JRE DRIVER
APP DB
CLIENT SERVER
JDBC Architecture
JDBC API Application
DB DB DB
JDBC
JDBC supports 4 types of Drivers
JDBC-ODBC BRIDGE
J O
JAVA D D
APP DB
B B
C C SERVER
CLIENT
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.
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
Class.forName(“DriverManagerClass”);
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()
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();
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);
• 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();