Date post: | 01-Jun-2018 |
Category: |
Documents |
Upload: | mishallmujahid |
View: | 241 times |
Download: | 0 times |
of 10
8/9/2019 Java 11 Jdbc
1/23
8/9/2019 Java 11 Jdbc
2/23
JDBC•
#$e JDBC % Java Data&ase Connectivit'(
• An AP t$at de)nes interfaces and classes forwriting data&ase applications in Java &'
ma*ing data&ase connections"
• +or*s wit$ almost an' relational data&ase"
• JDBC is a Java AP for e,ecuting S-.statements and supports &asic S-.functionalit'"
8/9/2019 Java 11 Jdbc
3/23
JDBC Arc$itecture
8/9/2019 Java 11 Jdbc
4/23
JDBC Basics
• import java.sql.*;
• Steps/" .oading a data&ase driver
0" Creating a 1d&c Connection
3" Creating a 1d&c Statement o&1ect2" ,ecuting a S-. statement wit$ t$e Statemento&1ect4 and returning a 1d&c resultSet
5" ,tract data from result set
6" Cleanup environment
8/9/2019 Java 11 Jdbc
5/23
Data&ase driver stringSamples
• m'S-. 7 com"m's8l"1d&c"Driver
•S8lserver 7com"microsoft"1d&c"s8lserver"S-.ServerDriver
• Apac$e Der&' 7 org"apac$e"der&'"1d&c"ClientDriver
• MSAccess %30 &it( 7 sun"1d&c"od&c"Jd&cOd&cDriver
• MSAccess %62 &it( 7Provider7Microsoft"AC"O.DB"/0"9 $ttp!::www"microsoft"com:downloads:details"asp,;
8/9/2019 Java 11 Jdbc
6/23
DB Connection .Samples
eneral format!
1d&c!s8lserver!::server!portData&aseEame7d&name
• mysql = jdbc:mysql://localhost/dbname
• Sql server = jdbc:microsot:sqlserver://localhost:!"##;databa
se$ame=dbname• ms access = jdbc:odbc:%mp
• Derby = jdbc:derby://localhost:!&'(/DBname
8/9/2019 Java 11 Jdbc
7/23
!. )oadin a database driver
tr' G
Class"forEame%HDB driver string pat$I(
Catc$%,ception e(
G
S'stem"out"println%Huna&le to load t$e
driverI(
8/9/2019 Java 11 Jdbc
8/23
'. Creatin a jdbc Connection
try+
Connection dbCon = n,ll;
dbCon = Driver-anaer.etConnectionDB
conneciton 01) 2,ser2p34
5
catch S6)%7ception 7 4+
System.o,t.println Co,ldn8t et connection9 4;5
8/9/2019 Java 11 Jdbc
9/23
3" Creating a 1d&c Statement o&1ect
Statement stmt = dbCon.createStatement4;
2" ,ecuting a S-. statement
String s 7 KS.C# id4 )rst4 last4 age
8/9/2019 Java 11 Jdbc
10/23
&. %7tract data rom res,lt set
w$ile%rs"ne,t%( ( G
::etrieve &' column name
S'stem"out"print%Hid 7I Lrs"getnt%KidK( (
S'stem"out"print%Hage 7 H L rs"getnt%KageK( ( S'stem"out"print%Hfname 7 H Lrs"getString%K)rstK( (
S'stem"out"print%Hl name 7 H L
rs"getString%KlastK( (
8/9/2019 Java 11 Jdbc
11/23
S#P 6! Cleanup environmentrs"close%( :: close result set
stmt"close%( :: close d& statement
d&Con"close%( :: close d& connection
catc$%S-.,ception se( ::andle errors for JDBC
G se"printStac*#race%(
catc$%,ception e( ::andle errors for Class"forEame
G e"printStac*#race%(
)nall'G ::)nall' &loc* used to close resources
if%stmtN7null( stmt"close%(
if%d&ConN7null(
conn"close%(
:: end )nall'
8/9/2019 Java 11 Jdbc
12/23
Complete Code Structuretry+
static nal Strin DBDriver = DB Driver strin;
static nal Strin DBCon = DB connection strin;
Strin sql6,ery = 1?- %mployees
8/9/2019 Java 11 Jdbc
13/23
JDBC Statement #'pes
• Statement
•
@repared Statement
• Callable Statement
8/9/2019 Java 11 Jdbc
14/23
Statement:
,ecute simple s8l 8ueries wit$out parameters"S'nta, ! Statement createStatement%(
@repared Statement: ,ecute precompiled s8l 8ueries wit$ or wit$outparameters"S'nta, ! PreparedStatementprepareStatement%String s8l(
Callable Statement: ,ecute a call to a data&ase stored procedure"
S'nta, ! Calla&leStatement prepareCall%String s8l(
8/9/2019 Java 11 Jdbc
15/23
Statement• se for generalpurpose access to 'our data&ase"
•
,ecute simple s8l 8ueries wit$out parameters"• 'ou can t$en use it to e,ecute a S-. statement wit$ one of
its t$ree e,ecute met$ods"
• Synta7 : – stmt 7 conn"createStatement% ( – stmt"close%(
• 'ou can t$en use it to e,ecute a S-. statement wit$ one of its t$reee,ecute met$ods"
• boolean e7ec,teStrin S6)4 !
– se t$is met$od to e,ecute S-. DD. statements or w$en 'ou needto use trul' d'namic S-."
• int e7ec,te0pdateStrin S6)4 : – se t$is met$od to e,ecute S-. statements for w$ic$ 'ou e,pect
to get a num&er of rows aected for e,ample4 an ES#4PDA#4 or D.# statement"
• 1es,ltSet e7ec,te6,eryStrin S6)4 :
– eturns a esultSet o&1ect" se t$is met$od w$en 'ou e,pect toget a result set4 as 'ou would wit$ a S.C# statement"
8/9/2019 Java 11 Jdbc
16/23
esult Sets Details
• A esultSet is a ta&ular structurecontaining t$e dataset returned &'e,ecuting a 8uer' statement"
• t contains &ot$ t$e data itself andmetadata"
• A esultSet o&1ect is readonl' andforwardonl' &' default"
8/9/2019 Java 11 Jdbc
17/23
esult Set cont""
• #$e esultSet o&1ect provides t$e necessar'met$ods to! – iterate over t$e rows &ot$ forward and &ac*ward"
–
read or update eac$ column content" – insert new rows into t$e esultSet and )nall' into
t$e underl'ing ta&le"
– delete rows from t$e esultSet and t$eunderl'ing ta&le"
– ead t$e metadata associated wit$ t$e esultSet
– 1ump &etween rows &' providing t$e a&solute rownum&er"
8/9/2019 Java 11 Jdbc
18/23
esultSet ow #o;
1es,ltSet-etaData rsmd = n,ll;
• Ao3 to et total col,mns ret,rned in a1es,ltSet – rsmd"getColumnCount%(
• Ao3 to et col,mn names or ro3s ret,rned ina 1es,ltSet
– rsmd 7 rs"getMetaData%( – String value 7 rsmd"getColumnEame%/(
8/9/2019 Java 11 Jdbc
19/23
esultSet ow #o;•
Ao3 do create an ,pdatable1es,ltSetSome conditions appl' to t$e updata&leesultSet! –
#$e S.C# statement must $ave a single underl'ing ta&leno 1oins and no aggregation functions are allowed"
– #$e P of t$e underl'ing ta&le must &e present in t$eesultSet so it can propagate t$e c$anges to t$e correctrow in t$e ta&le"
Statement stmt 7connection"createStatement% esultSet"#QPRSCO..RSES#V4esultSet"COECRPDA#AB.(
8/9/2019 Java 11 Jdbc
20/23
esultSet ow #o;
• Ao3 can ,pdate a 1es,ltSetprorammatically
Statement stmt 7
conn"createStatement%esultSet"#QPRSCO..RSES#V4 esultSet"COECRPDA#AB.(
esultSet rs 7 stmt"e,ecute-uer'%KS.C# name
4lastEame 4sn
8/9/2019 Java 11 Jdbc
21/23
Create StatementArguments
• #$e )rst argument in CreateStatement indicates t$e t'pe oft$e esultSet o&1ect ! – E@%F>?1GH1DF?$)E: #$e esultSet will onl' allow t$e cursor to go forward"
– E@%F SC1?))F$S%$SI%: #$e cursor can scroll forwards and&ac*wards4 and t$e result set is not sensitive to c$anges made &' ot$ers
to t$e data&ase t$at occur after t$e result set was created" – E@%FSC1?))FS%$SI%: #$e cursor can scroll forwards and
&ac*wards4 and t$e result set is sensitive to c$anges made &' ot$ers tot$e data&ase t$at occur after t$e result set was created"
• #$e second argument in CreateStatement indicates w$et$er
t$e esultSet is updata&le or not" #$e following values areaccepta&le for t$is parameter! – C?$C01F1%HDF?$)E: A esultSet of t$is t'pe is readonl'
– C?$C01F0@DHHB)% : +e can update t$e resultset created wit$ t$is valuefor t$e mentioned parameter"
8/9/2019 Java 11 Jdbc
22/23
• +e can iterate forward in t$is esultSetusing t$e following snippet
w$ile %rs"ne,t%(( G
String name 7 rs"getString%KnameK(
String lastEame 7 rs"getString %KlastEameK( String sn 7 rs"getString%HsnI(
S'stem"out"println%name L K K L lastEame L H I Lsn(
• se rs"previous%( instead of rs"ne,t toiterate &ac*word"
• Delete a speci)c row – rset"a&solute%5(
– rset"deleteow%(
8/9/2019 Java 11 Jdbc
23/23
• #o e,ecute a 8uer' to update a ta&leor delete some row in t$e ta&le use
esultset"e,ecuteupdate%( function"