Sie sind auf Seite 1von 89

Advanced Java Programming

Topic: JDBC
By
Ravi Kant Sahu
Asst. Professor
ove!y
Professiona!
"niversity#
Pun$a%
&ut!ines
1 'ntroduction
2 Re!ationa! Data%ase System
3 JDBC
4 JDBC Architecture
5 JDBC 'nterfaces
6 Prepared Statements
7 Ca!!a%!e Statement
8 Retrieving (etadata
9 Scro!!a%!e and "pdata%!e Resu!tSet
10 Batch Processing
Ravi Kant Sahu# Asst. Professor )
ove!y Professiona! "niversity# Pun$a%
*'ndia+
'ntroduction
1 Data%ase System consists of :
1. A data%ase
2. A soft,are that stores and manages data in the data%ase
3. App!ication programs that present the data and ena%!e the
user to interact ,ith the data%ase system.
Application User
System User
Application Programs
Database Management
System
Database
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Re!ationa! Data%ase System
1 Based on Re!ationa! Data (ode!.
2 Key Components are:
1. Structure
2. 'ntegrity
3. anguage
3 Structure defines the representation of data.
4 Integrity imposes constraints on data.
5 Language provides means for accessing and manipu!ating the data.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Re!ationa! Structure
1 A Re!ation is a ta%!e consisting of non-dup!icate ro,s.
2 A Ro, of a ta%!e represents a Record# and ca!!ed as a Tup!e.
3 A Co!umn in a ta%!e represents an Attri%ute.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
'ntegrity Constraints
1 An 'ntegrity Constraint imposes a condition that a!! the
!ega! va!ues in a ta%!e must satisfy.
2 Three types of Constraints are:
1. Domain Constraint
2. Primary Key Constraints
3. .oreign Key Constraints
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
'ntegrity Constraints
1 Domain Constraints specify the permissi%!e va!ues for any attri%ute.
2 Primary Key Constraint: Primary Key is a minima! Super /ey# used
to identify a tup!e in the data%ase.
.oreign Key Constraint: defines the re!ationship among
re!ations *ta%!es+.
1 A set of attri%utes F is a foreign /ey in any re!ation R that
references re!ation T if:
1. The attri%utes in F have the same domain as the primary /ey in T.
2. A non-nu!! va!ue on F in R must match a primary /ey va!ue in T.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
JDBC
1 JDBC stands for Java DataBase Connectivity.
2 JDBC is a standard Java AP' for data%ase-independent
connectivity %et,een the Java programming !anguage
and a ,ide range of data%ases.
3 By using JDBC# a Java app!ication can access any /ind of
ta%u!ar data# especia!!y data stored in a Re!ationa! Data%ase.
4 JDBC ,or/s ,ith Java on a variety of p!atforms# such as
0indo,s# (ac &S# and the various versions of "1'2.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
JDBC
1 A JDBC %ased app!ication is insu!ated from the
characteristics of specific data%ase engines.
Java Application
JDBC
Access Oracle Sybase
Database Database
Databa
se
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Pro%!em ,ith Data%ase Pac/ages
1 Different 3endors provided Data%ase Pac/ages ,ith
different AP's *functions defined for App!ication+ in
different Techno!ogies.
2 The Pro%!em ,ith native drivers ,as that a programmer
has to !earn a ne, AP' for each Data%ase and app!ication
needs to %e modified if data%ase pac/age is changed.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Data%ase Pac/ages and App!ication
Ravi Kant Sahu# Asst. Professor )
ove!y Professiona! "niversity# Pun$a%
*'ndia+
&DBC
1 &DBC *&pen DataBase Connectivity+ provides the
so!ution of that pro%!em.
2 't is a common AP' that is supported %y a!! data%ases.
3 &DBC represents function prototype in C.
4 .rom (icrosoft# 4556
5 7uite easy to use *preferred in .1et+8
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
&DBC
Ravi Kant
Sahu# Asst.
Professor )
ove!y
Professiona!
"niversity#
Pun$a% *'ndia+
JDBC Concepts
1 JDBC9s design is very simi!ar to the design of &DBC.
2 Data%ase vendors provide JDBC drivers for the
data%ase pac/age.
3 JDBC he!ps in ,riting Java app!ications that manage
these three programming activities:
1 Connect to a data source# !i/e a data%ase
2 Send :ueries and update statements to the
data%ase
3 Retrieve and process the resu!ts received from the
data%ase in ans,er to our :uery
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
JDBC Concepts
1 Driver (anager
1 oads data%ase drivers# and manages the
connection %et,een the app!ication and the driver
2 Driver
1 Trans!ates AP' ca!!s into operations for a specific data
source
3 Connection
1 A session %et,een an app!ication and a data%ase
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
JDBC Concepts
1 Statement
1 An S7 Statement to perform a :uery or update
operation
2 (etadata
1 'nformation a%out returned data# the data%ase and the
driver
3 Resu!tSet
1 ogica! set of co!umns and ro,s returned %y
e;ecuting an S7 statement *resu!ting tup!es+
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Steps during <;ecution
1 The fo!!o,ing steps are e;ecuted ,hen running a JDBC app!ication
1 'mport the necessary c!asses
2 oad the JDBC driver
3 'dentify the data%ase source
4 A!!ocate a =connection> o%$ect *create+
5 A!!ocate a =Statement> o%$ect *create+
6 <;ecute a :uery using the =Statement> o%$ect
7 Retrieve data from the returned =Resu!tSet> o%$ect
8 C!ose the =Resu!tSet> o%$ect
9 C!ose the =Statement> o%$ect
10 C!ose the =Connection> o%$ect
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
JDBC Component 'nteraction
Driver
Manager
Creates
Connection
Creates
Statement
Creates

ResultSet
SQL
Driver
Establish
Link to DB
Result
(tuples)
Database
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
Steps 'nvo!ved in Basic JDBC &perations
1. Load the JDBC driver class:
C!ass.for1ame*=driver1ame>+?
2. pen a database connection:
Driver(anager.getConnection
*=$d%c:;;;:datasource>+?
3. !ssue SQL state"ents:
stmt @ con.createStatement*+?
stmt.e;ecute7uery *=Se!ect A from
myTa%!e>+?
#$ %rocess
resultset:
,hi!e
*rs.ne;t*++ B
na
me
@
rs.g
etSt
ring
*=na
me>
+?
am
oun
t @
rs.g
et'n
t*=a
m
t
>
+
?

C
Driver &ana'er
Driver
Connection
Database
State"ent
Result Set
Ravi Kant Sahu# Asst.
Professor ) ove!y
Professiona!
"niversity#
Pun$a% *'ndia+
JDBC
ARCHITECTURE
T,o-Tier Data%ase Access (ode!
1 Java App!ication ta!/s
direct!y to the data%ase.
2 Accomp!ished through the
JDBC driver ,hich sends
commands direct!y to the
data%ase.
3 Resu!ts sent %ac/ direct!y to
the app!ication
Applicat
ion
Space
Java
Applica
tion
JDBC
Drive
r
SQL
Resul
t
Comma
nd Set
Databas e
Ravi Kant
Sahu#
Asst.
Prof
ess
or ) ove!y Professiona!
"niversity# Pun$a% *'ndia+
Three-Tier Data%ase Access (ode!
1JDBC driver sends commands
to a midd!e tier# ,hich in turn
sends commands to data%ase.
2Resu!ts are sent %ac/ to the
midd!e tier# ,hich
communicates them %ac/ to the
app!ication
Applicatio
n Space
Java
Applic
ation
JDBC
Driv
er
SQL
Resul
Comman
d
Appl
icati
on
Serv
er
(m
id
dl
e
tie
r!
"ropriet
ary
"rotoco
l
Database
Ravi Kant
Sahu# Asst.
Prof
ess
or ) ove!y Professiona!
"niversity# Pun$a% *'ndia+
JDBC
DRIVERS
JDBC Driver Types
1 JDBC-&DBC Bridge# p!us &DBC driver *Type 4+
1 JDBC methods -D Trans!ate JDBC methods to &DBC
methods -D &DBC to native methods -D 1ative methods AP'
2 1ative-AP'# part!y Java driver *Type E+
1 JDBC methods -D (ap JDBC methods to native methods
*ca!!s to vendor !i%rary+ -D 1ative methods AP' *vendor !i%rary+
3 JDBC-1et,or/ driver *Type F+
1 JDBC methods -D Trans!ate to 1ative AP' methods
through TCPG'P net,or/ -D 1ative AP' methods
4 Pure Java driver *Type 6+
1 Java methods -D 1ative methods in Java
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Type 4: JDBC-&DBC Bridge
1This driver type
is provided %y
Sun ,ith JDK
2Provides JDBC
access to
data%ases
through &DBC
drivers
3&DBC driver
must %e
configured for
the %ridge to
,or/
4&n!y
so!ution
if no
JDBC
driver
avai!a%!
e for
the
DB(S
App
lica
tion
Spa
ce
Jav
a
Ap
pli
cat
ion
JDBC

!DB
C
Bri"ge
SQL
Resul
Comman Set
d
ODB
C
Driv
er
"ropriet
ary
"rotoc
ol
Databas e
Ravi Kant
Sahu# Asst.
Professor
) ove!y
Pr
ofe
ssiona! "niversity#
Pun$a% *'ndia+
Type E: 1ative-AP'# Part!y Java Driver
1 1ative-AP' driver converts
JDBC commands into DB(S-
specific native ca!!s
2 Direct!y interfaces ,ith the
data%ase
Applica
tion
Space
Java
Applic
ation
#ype $
JDBC
Driver
SQL Resul
t
Comman
d Set
%
a
ti
v
e
Da
ta
ba
se
Library
"roprietar
y
"rot
ocol
Database
Ravi Kant
Sahu# Asst.
Professor )
ov
e!y
Professiona! "niversity#
Pun$a% *'ndia+
Type F: JDBC-1et,or/ Driver
1Trans!ates
JDBC ca!!s
into a
data%ase-
independent
net,or/
protoco! and
sent to a
midd!e,are
server.
2This server
trans!ates this
DB(S-
independent
protoco! into a
DB(S-
specific
protoco!
and sent to
the
data%ase.
3Resu!ts sent
%ac/ to the
midd!e,are
and routed
to the c!ient
App
licat
ion
Spa
ce
Jav
a
Ap
pli
cat
ion
#ype
&
JDB
C Driver
SQL
Resul
t
Comman
d Set
Middle
'are
Space
JDBC
Driv
er
"ropriet
ary
"roto
col
Database
Ravi Kant
Sahu#
Asst.
Professor
) ove!y Professiona!
"niversity# Pun$a%
*'ndia+
Type 6: 1ative-Protoco!# Pure Java Driver
1Pure Java drivers that
communicate direct!y ,ith
the vendor9s data%ase
2JDBC commands converted
to data%ase engine9s native
protoco! direct!y
Application
Space
Java
Application
#ype
(
JDBC
Drive
r
Advantage: no additiona!
trans!ation or midd!e,are !ayer
'mproves performance
Database
Ravi Kant
Sa
hu#
Asst. Professor ) ove!y
Professiona! "niversity#
Pun$a% *'ndia+
Driver (anager
1 The Driver(anager c!ass is responsi%!e for esta%!ishing connections
to the data sources# accessed through the JDBC drivers.
2 The driver can %e !oaded e;p!icit!y %y ca!!ing the static method
=for1ame> in the =C!ass> c!ass and pass the driver argument
C!ass.for1ame*=sun.$d%c.od%c.Jd%c&d%cDriver>+?
1 The =for1ame*+> method can thro, a
ClassNotFoundException if the driver c!ass is not found.
Hence# this function ca!! shou!d %e in a try-catch %!oc/.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Connection &%$ect
1 Connection o%$ect represents an esta%!ished connection to a
particu!ar data source
2 A connection o%$ect can a!so %e used to :uery the data source
*data and metadata+
3 Different versions of getConnection*+ method contained in
the Driver(anager c!ass that returns a connection o%$ect:
1 Connection con @ Driver(anager.getConnection*source+?
2 Connection con @ Driver(anager.getConnection*source# uIname#
p,d+?
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Statement &%$ect
1 "sed for creating an S7 :uery# e;ecute it# and retrieve the resu!ts.
2 Statement o%$ects are created %y ca!!ing the createStatement()
method of a va!id connection o%$ect.
3 <;ecutes S7 :uery %y ca!!ing the executeuery() method.
4 The S7 :uery string is passed as argument to the executeuery() method
5 The resu!t of e;ecuting the :uery is returned as an o%$ect
of type =Resu!tSet>
Statement stmt # con$createStatement%&'
Res(ltSet myres(lts # stmt$e)ec(te*(ery%+select , -rom
a(t.ors/&'
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Resu!tSet &%$ect
1 The resu!ts of e;ecuting an S7 :uery are returned in the
form of an o%$ect that imp!ements the Resu!tSet interface.
2 Resu!tSet o%$ect contains a =cursor> that points to a particu!ar
record *ca!!ed the current record+.
3 0hen the Resu!tSet o%$ect is created# the cursor points to the
position immediate!y preceding the first record.
4 Severa! methods avai!a%!e to navigate the Resu!tSet %y
moving the cursor:
1 first*+# !ast*+# %efore.irst*+# afterast*+# ne;t*+#
previous*+# etc. GGreturns true if the move is successfu!
2 is.irst*+ GG,hether you reached the %eginning of the
Resu!tSet
3 isast*+ GG ,hether you reached the end of the Resu!tSet
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Accessing Data in a Resu!tSet
1 (ethods for Retrieving Co!umn Data:
1 getString*+# get'nt*+# getShort*+# get.!oat*+#
getDou%!e*+# getTime*+ etc.
2 0e can a!,ays use getString() method for numerica!
va!ues if ,e are not going to do some computations.
3 Co!umn names are 1&T case sensitive.
4 Resu!tSet(etaData o%$ect has metadata information
a%out records# i.e.# co!umn names# data types etc.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Statement
Statement
1 Defines the methods and properties that ena%!e us to send
S7 or PGS7 commands and receive data from data%ase.
2 Statement: "sefu! ,hen ,e are using static S7 statements at runtime.
1- The Statement interface cannot accept parameters.
3 PreparedStatement: "sed ,hen ,e p!an to use the S7
statements many times.
1- The PreparedStatement interface accepts input parameters at runtime.
4 Ca!!a%!eStatement: "sed ,hen ,e ,ant to access
data%ase stored procedures.
1- The Ca!!a%!eStatement interface can a!so accept
runtime input parameters.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Statement (ethods
%oo!ean e;ecute*String S7+: Returns a %oo!ean va!ue of true if
a Resu!tSet o%$ect can %e retrieved? other,ise# it returns fa!se.
-- "se this method to e;ecute S7 DD statements.
int e;ecute"pdate*String S7+: Returns the num%ers of
ro,s affected %y the e;ecution of the S7 statement.
1- "se this method to e;ecute S7 statements for ,hich you
e;pect to get a num%er of ro,s affected - for e;amp!e# an
'1S<RT# "PDAT<# or D<<T< statement.
Resu!tSet e;ecute7uery*String S7+: Returns a Resu!tSet o%$ect.
1- "se this method ,hen you e;pect to get a resu!t set# as you
,ou!d ,ith a S<<CT statement.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
PreparedStatement
1 The PreparedStatement interface extends t!e Statement inter"ace#
2 PreparedStatement provides the faci!ity of executing
parameteri$ed %uery#
3 prepareStatement() method of Connection interface is used
to create a prepared Statement.
Statement pstmt @ con.prepareStatement*=insert into
<mp *id# name# age+> J =va!ues *K# K# K+>+?
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
PreparedStatement (ethods
1 't a!so provides methods for setting the parameters
in the preparedStatement o%$ect.
Lenera! method Signature:
set222 *int parameterIndex# 2 &alue+
1 pstmt.set'nt *4# FM+
2 pstmt.setString *E# =Ravi Kant>+
3 pstmt.set'nt *F# EN+
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Ca!!a%!eStatement
1 Ca!!a%!eStatement interface is used to e;ecute S7-
stored procedures.
2 A Ca!!a%!eStatement o%$ect can %e created using the
prepareCall (String call) method.
Ca!!a%!eStatement cstmt @ con.prepareCa!!
*=Bca!! samp!eProcedure*K# K# K+C>+?
Ca!!a%!eStatement for a function:
Ca!!a%!eStatement cstmt @ con.prepareCa!!
*=BK @ ca!! samp!eProcedure*K# K# K+C>+?
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
StoredProcedure
create or rep!ace procedure samp!eProcedure
*p4 in varchar# pE out num%er# pF in out
integer+ is %egin
-- do something
end samp!eProcedure?
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
Ca!!a%!eStatement (ethods
Setting '1 Parameter:
set222 *int parameterIndex# 2 &alue+
1 cstmt.set'nt *4# FM+
2 cstmt.setString *E# =Ravi Kant>+
Setting &"T Parameter:
register&utParameter *int parameter'nde;# Types.222+
-- Types.'1T<L<R# Types.D&"B< etc
cstmt.register&utParameter *F# Types.'1T<L<R+?
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Resu!tset
Resu!tSet
1 A Resu!tSet o%$ect maintains a cursor pointing to its
current ro, of data.
2 'nitia!!y the cursor is positioned %efore the first ro,.
3 The ne;t method moves the cursor to the ne;t ro,# and returns
fa!se ,hen there are no more ro,s in the Resu!tSet o%$ect.
4 A defau!t Resu!tSet o%$ect is not updata%!e and has a
cursor that moves for,ard on!y.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Scro!!a%!e and "pdatea%!e Resu!tset
1 0it. t.e ne1 2ersions o- JDBC3 1e can scroll t.e
ro1s bot. -or1ar" an" bac41ar" an" mo2e t.e
c(rsor to a "esire" location (sing t.e
frst(), last(), next(), previous(),
absolute(), or relative() met.o"$
1 0e can insert3 "elete3 or (p"ate a ro1 in t.e
res(lt set an" .a2e t.e c.anges a(tomatically
re5ecte" in t.e "atabase$
2 To obtain a scrollable or (p"atable res(lt set3
6rst create a statement 1it. an appropriate
type an" conc(rrency mo"e$
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
Scro!!a%!e and "pdatea%!e Resu!tset
7or static statement3
Statement statement #
connection$createStatement %
int resultSet#ype)
int
resultSetConcurrency!*
7or a prepare" statement3
Prepare"Statement statement #
connection$prepareStatement%
String s+l)
int resultSet#ype)
int resultSetConcurrency!*
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
Scro!!a%!e Resu!tset
1 T.e possible 2al(es o- res(ltSetType are t.e
constants "e6ne" in t.e Res(ltSet8
2 T9PE:7!R0ARD:!;<98 T.e res(lt set is
accesse" -or1ar" se=(entially$
3 T9PE:SCR!<<:I;SE;SITIVE8 T.e res(lt set is
scrollable3 b(t not sensiti2e to c.anges in t.e
"atabase$
4 T9PE:SCR!<<:SE;SITIVE8 T.e res(lt set is
scrollable an" sensiti2e to c.anges ma"e by
ot.ers$
5 Use t.is type i- yo( 1ant t.e res(lt set to be
scrollable an" (p"atable$
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
"pdatea%!e Resu!tset
1 T.e possible 2al(es o- res(ltSetConc(rrency are
t.e constants "e6ne" in t.e Res(ltSet8
2 C!;CUR:READ:!;<98 T.e res(lt set cannot be
(se" to (p"ate t.e "atabase$
3 C!;CUR:UPDATAB<E8 T.e res(lt set can be
(se" to (p"ate t.e "atabase$
4 7or e)ample3 scrollable an" (p"atable res(ltset can
be create" as -ollo1s8
Statement statement # connection$createStatement
%Res(ltSet$T9PE:SCR!<<:SE;
SITIVE3
Res(ltSet$C!;CUR:UPDATAB<
E&
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Retrieving (etaData
(etaData
1 JDBC provides t,o interfaces for o%taining metadata.
2 Data%ase(etaData interface for o%taining data%ase information.
3 Resu!tSet(etaData interface for o%taining information of
specific Resu!tSet *e.g. co!umn count and co!umn names+.
4 get'eta(ata() method is used to o%tain the metadata.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Data%ase(etaData
1 .o!!o,ing methods can %e used to o%tain data%ase metadata:
1. get"ser1ame*+
2. get"R*+
3. getData%aseProduct1ame*+
4. getData%aseProduct3ersion*+
5. getDriver1ame*+
6. getDriver3ersion*+
7. getDriver(a$or3ersion*+
8. getDriver(inor3ersion*+
9. get(a;Connections*+
10. get(a;Ta%!e1ameength*+
11. get(a;Co!umns'nTa%!e*+
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Resu!tSet(etaData
1 't provides the fo!!o,ing methods:
1. getCo!umn1ame*int+ method to find the co!umn names.
2. getCo!umnCount*+ method to find the num%er of co!umns in the
resu!tset.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
Batch Processing
Batch Processing
1 Batc. Processing allo1s to gro(p relate" S*<
statements into a batc. an" s(bmit t.em 1it.
one call to t.e "atabase$
2 0.en 1e sen" se2eral S*< statements to t.e
"atabase at once3 it re"(ces t.e amo(nt o-
comm(nication o2er.ea"3 t.ereby impro2ing
per-ormance$
3 DatabaseMetaData.supportsBatchUpdates()
met.o" is (se" to "etermine i- t.e target
"atabase s(pports batc. (p"ate processing$
4 T.e met.o" ret(rns true i- JDBC "ri2er s(pports
t.is -eat(re$
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
(ethods for Batch Processing
1 T.e addBatc,(! met.o" is (se" to a"" in"i2i"(al
statements to t.e batc.$
2 T.e e-ecuteBatc,(! is (se" to start t.e e)ec(tion
o- all t.e statements gro(pe" toget.er$
3 T.e e-ecuteBatc,(! ret(rns an array o- integers3
an" eac. element o- t.e array represents t.e (p"ate
co(nt -or t.e respecti2e (p"ate statement$
4 T.e clearBatc,(! met.o" is (se" to remo2e all t.e
statements a""e" 1it. t.e a""Batc.%& met.o"$
5 0e can not selecti2ely c.oose 1.ic. statement to
remo2e$
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
Steps to use Batch Processing
1 Create a Statement ob>ect (sing
createStatement() met.o"s$
2 Set a(to?commit to -alse (sing setAutoCommit()$
3 A"" as many as S*< statements yo( li4e into
batc. (sing addBatch() met.o" on create"
statement ob>ect$
4 E)ec(te all t.e S*< statements (sing
executeBatch() met.o" on create" statement
ob>ect$
5 7inally3 commit all t.e c.anges (sing commit()
met.o"$
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
<;amp!e
@@ Create statement ob>ect
Statement stmt #
conn$createStatement%&' @@ Set a(to?commit
to -alse
conn$setA(toCommit%
-alse&' @@ Create some S*< statements an"
a"" t.em in t.e batc.
String S*< # AI;SERT I;T! Emp %i"3 name3 age& A B
AVA<UES%C3DRa2iE3 FG&A' stmt$a""Batc.%S*<&'
String S*< # AI;SERT I;T! Emp %i"3 name3 age& A B
AVA<UES%FHH3IRa>I3 JH&A' stmt$a""Batc.%S*<&'
String S*< # AUPDATE Emp SET age # JG A B
A0HERE i" # CHHA' stmt$a""Batc.%S*<&'
@@ Create an intKL to .ol" ret(rne" 2al(es
intKL co(nt #
stmt$e)ec(teBatc.%&' @@E)plicitly commit
statements to apply c.anges
conn$commit%&'
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
<oa"in
g
Deve!oping JDBC Programs
Statement to !oad a driver:
"r
i2
er
s
Class$(or)a"e(*JDBCDriv
erClass*)+
E is a c!ass. .or e;amp!e:
connections
C
r
e
a
t
ing an"
e)ec(ting
statements
Processing
Res(ltSet
Database
Access
(yS7
&rac!e
1
Th
e
J
D
BC-
&DBC
driver for
Access is
%und!ed
in JDK.
2
(
y
S
7
driver c!ass is
in mys:!$d%c.$ar
3 &rac!e driver
c!ass is in
c!asses4E.$ar
Ravi Kant
Sahu# Asst.
Pr
of
essor ) ove!y
Professiona!
"niversity# Pun$a%
*'ndia+
Deve!oping JDBC Programs
Connection connection #
Dri2erManager$getConnection%"atabaseUR<&'
<oa"ing "ri2ers
Establis.ing
connections
Creating an" e)ec(ting statements
Processing
Res(ltSet
Data
base
UR<
Patte
rn
Acce
ss
>"bc8
o"bc
8"ata
So(r
ce
MyS
*<
>"bc8
mys
=l8@@.
ostname@"bname
!racle
>"bc8oracle8t.in8M.ostna
me8portN8oracleDBSID
E)amples8
7or Access8
Connection connection
#
Dri2erManager$getCon
nection
%A>"bc8o"bc8E)ampleM
DBDataSo(rceA&'
7or MyS*<8
Connection
connection #
Dri2erManager$get
Connection
%A>"bc8mys=l8@@local.ost@testA&'
7or !racle8
Connection connection #
Dri2erManager$getConn
ection
%A>"bc8oracle8t.in8Mlian
g$armstrong$e"(8CGFC8o
rclA3 AscottA3 AtigerA&'
Ravi Kant Sahu# Asst. Professor )
ove!y Professiona! "niversity# Pun$a%
*'ndia+
<oa"ing
"ri2ers
Establis.i
ng
connecti
ons
Creating
an"
e)ec(ti
ng
statem
ents
Processin
g
Res(ltSet
Deve!oping JDBC Programs
Creating statement:
Statement statement @ connection.createStatement*+?
<;ecuting statement *for update# de!ete# insert+:
statement.e;ecute"pdate
*Ocreate ta%!e Temp *co!4 char*P+# co!E char*P++O+?
<;ecuting statement *for se!ect+:
GG Se!ect the co!umns from the Student
ta%!e Resu!tSet resu!tSet @
statement.e;ecute7uery
*Ose!ect first1ame# mi# !ast1ame from Student ,here
!ast1ame O J O @ QSmithQO+?
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+
<oa"ing
"ri2ers
Establis
.ing
connect
ions
Creating
an"
e)ec(ti
ng
statem
ents
Proces
sing
Res(lt
Set
Deve!oping JDBC Programs
<;ecuting statement *for se!ect+:
GG Se!ect the co!umns from the Student
ta%!e Resu!tSet resu!tSet @
stmt.e;ecute7uery
*Ose!ect first1ame# mi# !ast1ame from Student ,here
!ast1ame
O
J O @ QSmithQO+?
Processing Resu!tSet *for se!ect+:
GG 'terate through the resu!t and print the
student names ,hi!e *resu!tSet.ne;t*++
System.out.print!n*resu!tSet.getString*4
+ J O O J resu!tSet.getString*E+
J O. O J resu!tSet.getString*F++?
Ravi Kant Sahu# Asst. Professor ) ove!y
Professiona! "niversity# Pun$a% *'ndia+
JDBC connectivity ,ith &rac!e
&rac!e JDBC Thin Driver .ormat:
$d%c:orac!e:thin:)RhostD:RportD:RS'DD
$d%c:orac!e:thin:)GGRhostD:RportDGRserviceInameD
<;amp!e:
$d%c:orac!e:thin:45E.4NS.E.4:4PE4:2M4A
$d%c:orac!e:thin:)GG45E.4NS.E.4:4PE4G2<
1 The &rac!e System 'D *S'D+ is used to uni:ue!y identify a particu!ar
data%ase on a system. .or this reason# one cannot have more than
one data%ase ,ith the same S'D on a computer system.
2 1ote: Support for S'D is %eing phased out.
1 &rac!e recommends that users s,itch over to using service
names.
Ravi Kant Sahu# Asst. Professor ) ove!y Professiona! "niversity# Pun$a% *'ndia+

Das könnte Ihnen auch gefallen