ID8C 8as|cs Iava Database Connect|v|ty Steps flrsL lmporL Lhe [avasql package lmporL [avasql* 1he sLar ( * ) lndlcaLes LhaL all of Lhe classes ln Lhe package [avasql are Lo be lmporLed 1 Load|ng a database dr|ver Lry Classforname("sun[dbcodbc!dbcCdbcurlver") //Cr any oLher drlver
caLch(LxcepLlon x) SysLemouLprlnLln( unable Lo load Lhe drlver class!" )
we load Lhe drlver class by calllng Classforname() wlLh Lhe urlver class name as an argumenL 1he reLurn Lype of Lhe Classforname (SLrlng Classname) meLhod ls Class" Class ls a class ln [avalang package 2 Creat|ng a [dbc Connect|on 1he !u8C urlverManager class deflnes ob[ecLs whlch can connecL !ava appllcaLlons Lo a !u8C drlver urlverManager ls consldered Lhe backbone of !u8C archlLecLure urlverManager class manages Lhe !u8C drlvers LhaL are lnsLalled on Lhe sysLem lLs geLConnecLlon() meLhod ls used Lo esLabllsh a connecLlon Lo a daLabase lL uses a username password and a [dbc url Lo esLabllsh a connecLlon Lo Lhe daLabase and reLurns a connecLlon ob[ecL A [dbc ConnecLlon represenLs a sesslon/connecLlon wlLh a speclflc daLabase WlLhln Lhe conLexL of a ConnecLlon SCL L/SCL sLaLemenLs are execuLed and resulLs are reLurned A ConnecLlon ob[ecL provldes meLadaLa le lnformaLlon abouL Lhe daLabase Lables and flelds lL also conLalns meLhods Lo deal wlLh LransacLlons !u8C u8L SynLax [dbc subproLocol subname ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
2
We're uslng Lhe [dbc odbc subproLocol so Lhe urlverManager knows Lo use Lhe sun[dbcodbc!dbcCdbcurlver
3 Creat|ng a [dbc Statement ob[ect Cnce a connecLlon ls obLalned we can lnLeracL wlLh Lhe daLabase ConnecLlon lnLerface deflnes meLhods for lnLeracLlng wlLh Lhe daLabase vla Lhe esLabllshed connecLlon 1o execuLe SCL sLaLemenLs you need Lo lnsLanLlaLe a SLaLemenL ob[ecL from your connecLlon ob[ecL by uslng Lhe creaLeSLaLemenL() meLhod SLaLemenL sLaLemenL dbConnecLloncreaLeSLaLemenL() A sLaLemenL ob[ecL ls used Lo send and execuLe SCL sLaLemenLs Lo a daLabase 1hree klnds of SLaLemenLs Statement LxecuLe slmple sql querles wlLhouL parameLers SLaLemenL creaLeSLaLemenL() CreaLes an SCL SLaLemenL ob[ecL repared Statement LxecuLe precomplled sql querles wlLh or wlLhouL parameLers reparedSLaLemenL prepareSLaLemenL(SLrlng sql) reLurns a new reparedSLaLemenL ob[ecL reparedSLaLemenL ob[ecLs are precomplled SCL sLaLemenLs Ca||ab|e Statement LxecuLe a call Lo a daLabase sLored procedure CallableSLaLemenL prepareCall(SLrlng sql) reLurns a new CallableSLaLemenL ob[ecL CallableSLaLemenL ob[ecLs are SCL sLored procedure call sLaLemenLs 4 Lxecut|ng a SL statement w|th the Statement ob[ect and return|ng a [dbc resu|tSet SLaLemenL lnLerface deflnes meLhods LhaL are used Lo lnLeracL wlLh daLabase vla Lhe execuLlon of SCL sLaLemenLs 1he SLaLemenL class has Lhree meLhods for execuLlng sLaLemenLs execuLeCuery() execuLeupdaLe() and execuLe() lor a SLLLC1 sLaLemenL Lhe meLhod Lo use ls execuLeCuery lor sLaLemenLs LhaL creaLe or modlfy Lables Lhe meLhod Lo use ls execuLeupdaLe noLe SLaLemenLs LhaL creaLe a Lable alLer a Lable or drop a Lable are all examples of uuL sLaLemenLs and are execuLed wlLh Lhe meLhod execuLeupdaLe execuLe() execuLes an SCL sLaLemenL LhaL ls wrlLLen as SLrlng ob[ecL ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
3
kesu|tSet provldes access Lo a Lable of daLa generaLed by execuLlng a SLaLemenL 1he Lable rows are reLrleved ln sequence A 8esulLSeL malnLalns a cursor polnLlng Lo lLs currenL row of daLa 1he nexL() meLhod ls used Lo successlvely sLep Lhrough Lhe rows of Lhe Labular resulLs kesu|tSetMetaData lnLerface holds lnformaLlon on Lhe Lypes and properLles of Lhe columns ln a 8esulLSeL lL ls consLrucLed from Lhe ConnecLlon ob[ecL ketr|ev|ng Data us|ng ID8C Se|ect uery We can use Iava ID8C Se|ect statement ln a [ava program Lo reLrleve Lhe daLa and dlsplay lL for Lhe respecLlve 1ables !u8C reLurns resulLs ln a 8esulLSeL ob[ecL so we need Lo declare an lnsLance of Lhe class 8esulLSeL Lo hold our resulLs SelecL ls Lhe SCL keyword LhaL performs a query We lnvoke Lhe [dbc selecL query (execuLequery) meLhod uslng Lhe [dbc selecL daLa sLaLemenL as Lhe parameLer 1he Labular resulLs of Lhe query are capLured ln Lhe 8esulLSeL ob[ecL resulLs noLe LhaL execuLeCuery() always reLurns a 8esulLSeL alLhough lL need noL have any rows ln lL 1he reLurn value for an execuLeCuery ls a 8esulLSeL ob[ecL conLalnlng Lhe resulLs of Lhe query senL Lo Lhe u8MS 1o process each row ln Lhe 8esulLSeL we use Lhe nexL() meLhod 1hls meLhod moves Lhe polnLer Lhrough Lhe rows of daLa 1he 8esulLSeL malnLalns a cursor polnLlng Lo Lhe currenL row 8ecause Lhls cursor ls lnlLlally poslLloned before Lhe flrsL row we musL call nexL() before we can see any rows aL all 8elow ls a !u8C rogram showlng Lhe use of execuLeCuery() Lo reLrleve values from 8esulLSeLs uslng [dbc programmlng ID8C SL Se|ect and Update Lxamp|e lmporL [avaxswlng!CpLlonane lmporL [avasql* publlc class !u8Crogram
!CpLlonaneshowMessageulalog(null"!u8C rogrammlng showl ng 8eLrleval of 1able uaLa") ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
ID8C Update 1ab|e We can use [ava [dbc update statements ln a [ava program Lo updaLe Lhe daLa for a 1able ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
9
1he reLurn value for a [dbc sql updaLe ls an lnL LhaL lndlcaLes how many rows of a Lable were updaLed lor lnsLance ln a sLaLemenL llke lnL n sLmLexecuLeupdaLe()
Iava ID8C repared statements are precomplled SCL sLaLemenLs recomplled SCL ls useful lf Lhe same SCL ls Lo be execuLed repeaLedly for example ln a loop Lvery [ava sql prepared sLaLemenL ls complled aL some polnL 1o use a [ava preparedsLaLemenLs you musL flrsL creaLe a ob[ecL by calllng Lhe ConnecLlonprepareSLaLemenL() meLhod lf you wanL Lo execuLe a SLaLemenL ob[ecL many Llmes lL normally reduces execuLlon Llme Lo use a reparedSLaLemenL ob[ecL lnsLead
1he synLax ls sLralghLforward [usL lnserL quesLlon marks for any parameLers LhaL you'll be subsLlLuLlng before you send Lhe SCL Lo Lhe daLabase As wlLh CallableSLaLemenLs you need Lo call close() Lo make sure daLabase resources are freed as soon as posslble 8elow ls a !u8C rogram showlng Lhe use of [dbc prepared sLaLemenLs Lo lnserL daLa lnLo Lables uslng [dbc programmlng ?ou need Lo supply values Lo be used ln place of Lhe quesLlon mark placeholders (lf Lhere are any) before you can execuLe a reparedSLaLemenL ob[ecL ?ou do Lhls by calllng one of Lhe seLxxx meLhods deflned ln Lhe reparedSLaLemenL class 1here ls a seLxxx meLhod for each prlmlLlve Lype declared ln Lhe !ava programmlng language An example of a reparedSLaLemenL ob[ecL ls reparedSLaLemenL psLmL conprepareSLaLemenL("updaLe Crders seL pname ? where rod_ld ?") psLmLseLlnL(2 100) psLmLseLSLrlng(1 8ob") psLmLexecuLeupdaLe()
example publlc sLaLlc vold updaLeLmployeesrepared() ConnecLlon con geLConnecLlon() // creaLe prepared sLaLemenL Lry psLmL conprepareSLaLemenL ("updaLe Lmployees seL name ? where Lmployee_ld ?") psLmLseLSLrlng(1 hemanLhbob")// lndex sLarLs wlLh 1 psLmLseLlnL(2 6323) psLmLexecuLeupdaLe() psLmLclose() conclose() caLch(SCLLxcepLlon ex) SysLemerrprlnLln("SCLLxcepLlon " + exgeLMessage())
ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
1ypes of kesu|t Sets 1he 8esulLSeL lnLerface provldes meLhods for reLrlevlng and manlpulaLlng Lhe resulLs of execuLed querles and 8esulLSeL ob[ecLs can have dlfferenL funcLlonallLy and characLerlsLlcs 1hese characLerlsLlcs are resulL seL Lype resulL seL concurrency and cursor holdablllLy1he Lype of a 8esulLSeL ob[ecL deLermlnes Lhe level of lLs funcLlonallLy ln Lwo areas Lhe ways ln whlch Lhe cursor can be manlpulaLed and how concurrenL changes made Lo Lhe underlylng daLa source are reflecLed by Lhe 8esulLSeL ob[ecL 1he senslLlvlLy of Lhe 8esulLSeL ob[ecL ls deLermlned by one of Lhree dlfferenL 8esulLSeL Lypes 8esulLseL conLalns resulLs of Lhe SCL query 1here are 3 baslc Lypes of resulLseL 1L_ICkWAkD_CNL As name suggesL Lhls Lype can only move forward and are nonscrollable 1L_SCkCLL_INSLNSI1IVL 1hls Lype ls scrollable whlch means Lhe cursor can move ln any dlrecLlon lL ls lnsenslLlve whlch means any change Lo Lhe daLabase wlll noL show change ln Lhe resulLseL whlle lL open 1L_SCkCLL_SLNSI1IVL 1hls Lype allows cursor Lo move ln any dlrecLlon and also propagaLes Lhe changes done Lo Lhe daLabase
1he followlng llne of code lllusLraLes one way Lo creaLe a scrollable 8esulLSeL ob[ecL ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
11
SLaLemenL sLmL concreaLeSLaLemenL(8esulLSeL1?L_SC8CLL_SLnSl1lvL 8esulLSeLCCnCu8_8LAu_CnL?) 8esulLSeL srs sLmLexecuLeCuery("") 1he flrsL argumenL ls one of Lhree consLanLs added Lo Lhe 8esulLSeL Al Lo lndlcaLe Lhe Lype of a 8esulLSeL ob[ecL 1?L_lC8WA8u_CnL? 1?L_SC8CLL_lnSLnSl1lvL and 1?L_SC8CLL_SLnSl1lvL 1he second argumenL ls one of Lwo 8esulLSeL consLanLs for speclfylng wheLher a resulL seL ls readonly or updaLable CCnCu8_8LAu_CnL? and CCnCu8_uuA1A8LL lf you do noL speclfy any consLanLs for Lhe Lype and updaLablllLy of a 8esulLSeL ob[ecL you wlll auLomaLlcally geL one LhaL ls 1?L_lC8WA8u_CnL? and CCnCu8_8LAu_CnL?
kesu|t Set Methods nexL() moves Lhe cursor forward one row 8eLurns Lrue lf Lhe cursor ls now poslLloned on a row and false lf Lhe cursor ls poslLloned afLer Lhe lasL row prevlous() moves Lhe cursor backwards one row 8eLurns Lrue lf Lhe cursor ls now poslLloned on a row and false lf Lhe cursor ls poslLloned before Lhe flrsL row flrsL() moves Lhe cursor Lo Lhe flrsL row ln Lhe 8esulLSeL ob[ecL 8eLurns Lrue lf Lhe cursor ls now poslLloned on Lhe flrsL row and false lf Lhe 8esulLSeL ob[ecL does noL conLaln any rows lasL() moves Lhe cursor Lo Lhe lasL row ln Lhe 8esulLSeL ob[ecL 8eLurns Lrue lf Lhe cursor ls now poslLloned on Lhe lasL row and false lf Lhe 8esulLSeL ob[ecL does noL conLaln any rows beforellrsL() poslLlons Lhe cursor aL Lhe sLarL of Lhe 8esulLSeL ob[ecL before Lhe flrsL row lf Lhe 8esulLSeL ob[ecL does noL conLaln any rows Lhls meLhod has no effecL afLerLasL() poslLlons Lhe cursor aL Lhe end of Lhe 8esulLSeL ob[ecL afLer Lhe lasL row lf Lhe 8esulLSeL ob[ecL does noL conLaln any rows Lhls meLhod has no effecL relaLlve(lnL rows) moves Lhe cursor relaLlve Lo lLs currenL poslLlon absoluLe(lnL n) poslLlons Lhe cursor on Lhe nLh row of Lhe 8esulLSeL ob[ecL
ID8C Dr|ver 1ypes ID8C dr|vers are dlvlded lnLo four Lypes or levels 1he d|fferent types of [dbc dr|vers are 1ype 1 !u8CCu8C 8rldge drlver (8rldge) 1ype 2 naLlveAl/parLly !ava drlver (naLlve) 1ype 3 All!ava/neLproLocol drlver (Mlddleware) 1ype 4 All !ava/naLlveproLocol drlver (ure)
1ype 1 ID8CCD8C 8r|dge dr|ver 1he 1ype 1 drlver LranslaLes all !u8C calls lnLo Cu8C calls and sends Lhem Lo Lhe Cu8C drlver Cu8C ls a generlc Al ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
12
AdvanLage 1he !u8CCu8C 8rldge allows access Lo almosL any daLabase slnce Lhe daLabase's Cu8C drlvers are already avallable ulsadvanLages 1 Slnce Lhe 8rldge drlver ls noL wrlLLen fully ln !ava 1ype 1 drlvers are noL porLable 2 A performance lssue ls seen as a !u8C call goes Lhrough Lhe brldge Lo Lhe Cu8C drlver 2 Lhen Lo Lhe daLabase and Lhls applles even ln Lhe reverse process 1hey are Lhe slowesL of all drlver Lypes 1he cllenL sysLem requlres Lhe Cu8C lnsLallaLlon Lo use Lhe drlver 3 noL good for Lhe Web
1ype 2 Nat|veAI]part|y Iava dr|ver 1he dlsLlncLlve characLerlsLlc of Lype 2 [dbc drlvers are LhaL 1ype 2 drlvers converL !u8C calls lnLo daLabasespeclflc calls le Lhls drlver ls speclflc Lo a parLlcular daLabase Some dlsLlncLlve characLerlsLlc of Lype 2 [dbc drlvers are shown belowLxample Cracle wlll have oracle naLlve apl
AdvanLage 1he dlsLlncLlve characLerlsLlc of Lype 2 [dbc drlvers are LhaL Lhey are Lyplcally offer 8eLLer performance Lhan Lhe !u8CCu8C 8rldge as Lhe layers of communlcaLlon (Llers) are less ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
13
Lhan LhaL of 1ype 1 and also lL uses naLlve apl whlch ls uaLabase speclflc ulsadvanLage 1 naLlve Al musL be lnsLalled ln Lhe CllenL SysLem and hence Lype 2 drlvers cannoL be used for Lhe lnLerneL 2 Llke 1ype 1 drlvers lL's noL wrlLLen ln !ava Language whlch forms a porLablllLy lssue 3 lf we change Lhe uaLabase we have Lo change Lhe naLlve apl as lL ls speclflc Lo a daLabase 4 usually noL Lhread safe
1ype 3 A|| Iava]Netprotoco| dr|ver 1ype 3 daLabase requesLs are passed Lhrough Lhe neLwork Lo Lhe mlddleLler server 1he mlddleLler Lhen LranslaLes Lhe requesL Lo Lhe daLabase lf Lhe mlddleLler server can ln Lurn use 1ype1 1ype 2 or 1ype 4 drlvers
AdvanLage 1 1hls drlver ls serverbased so Lhere ls no need for any vendor daLabase llbrary Lo be presenL on cllenL machlnes 2 1hls drlver ls fully wrlLLen ln !ava and hence orLable lL ls sulLable for Lhe web 3 1here are many opporLunlLles Lo opLlmlze porLablllLy performance and scalablllLy 4 1he neL proLocol can be deslgned Lo make Lhe cllenL !u8C drlver very small and fasL Lo load 3 1he Lype 3 drlver Lyplcally provldes supporL for feaLures such as cachlng (connecLlonsquery resulLs and so on) load balanclng and advanced sysLem admlnlsLraLlon such as logglng and audlLlng 6 1hls drlver ls very flexlble allows access Lo mulLlple daLabases uslng one drlver 7 1hey are Lhe mosL efflclenL amongsL all drlver Lypes ulsadvanLage lL requlres anoLher server appllcaLlon Lo lnsLall and malnLaln 1raverslng Lhe recordseL may Lake longer slnce Lhe daLa comes Lhrough Lhe backend server
1ype 4 Nat|veprotoco|]a||Iava dr|ver 1he 1ype 4 uses [ava neLworklng llbrarles Lo communlcaLe dlrecLly wlLh Lhe daLabase server ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
14
AdvanLage 1 1he ma[or beneflL of uslng a Lype 4 [dbc drlvers are LhaL Lhey are compleLely wrlLLen ln !ava Lo achleve plaLform lndependence and ellmlnaLe deploymenL admlnlsLraLlon lssueslL ls mosL sulLable for Lhe web 2 number of LranslaLlon layers ls very less le Lype 4 !u8C drlvers don'L have Lo LranslaLe daLabase requesLs Lo Cu8C or a naLlve connecLlvlLy lnLerface or Lo pass Lhe requesL on Lo anoLher server performance ls Lyplcally qulLe good 3 ?ou don'L need Lo lnsLall speclal sofLware on Lhe cllenL or server lurLher Lhese drlvers can be downloaded dynamlcally ulsadvanLage WlLh Lype 4 drlvers Lhe user needs a dlfferenL drlver for each daLabase
[avasq| Interface kesu|tSetMetaData pub||c |nterface kesu|tSetMetaData An ob[ect that can be used to get |nformat|on about the types and propert|es of the co|umns |n a kesu|tSet ob[ect 1he fo||ow|ng code fragment creates the kesu|tSet ob[ect rs creates the kesu|tSetMetaData ob[ect rsmd and uses rsmd to f|nd out how many co|umns rs has and whether the f|rst co|umn |n rs can be used |n a WnLkL c|ause kesu|tSet rs stmtexecuteuery(SLLLC1 a b c IkCM 1A8LL2) kesu|tSetMetaData rsmd rsgetMetaData() |nt numberCfCo|umns rsmdgetCo|umnCount() boo|ean b rsmd|sSearchab|e(1)
Method Deta|| 1 getCo|umnCount pub||c |nt getCo|umnCount() throws SLLxcept|on keturns the number of co|umns |n th|s kesu|tSet ob[ect keturns the number of co|umns 2 |sAutoIncrement pub||c boo|ean |sAutoIncrement(|nt co|umn) throws SLLxcept|on Ind|cates whether the des|gnated co|umn |s automat|ca||y numbered thus readon|y arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns true |f so fa|se otherw|se
3 |sCaseSens|t|ve pub||c boo|ean |sCaseSens|t|ve(|nt co|umn) throws SLLxcept|on Ind|cates whether a co|umns case matters arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns true |f so fa|se otherw|se 4 |sSearchab|e ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
16
pub||c boo|ean |sSearchab|e(|nt co|umn) throws SLLxcept|on Ind|cates whether the des|gnated co|umn can be used |n a where c|ause arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns true |f so fa|se otherw|se S |sCurrency pub||c boo|ean |sCurrency(|nt co|umn) throws SLLxcept|on Ind|cates whether the des|gnated co|umn |s a cash va|ue arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns true |f so fa|se otherw|se 1hrows SLLxcept|on |f a database access error occurs 6 |sNu||ab|e pub||c |nt |sNu||ab|e(|nt co|umn) throws SLLxcept|on Ind|cates the nu||ab|||ty of va|ues |n the des|gnated co|umn arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns the nu||ab|||ty status of the g|ven co|umn one of co|umnNoNu||s co|umnNu||ab|e or co|umnNu||ab|eUnknown 1hrows SLLxcept|on |f a database access error occurs 7 |sS|gned pub||c boo|ean |sS|gned(|nt co|umn) throws SLLxcept|on Ind|cates whether va|ues |n the des|gnated co|umn are s|gned numbers arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns true |f so fa|se otherw|se 1hrows SLLxcept|on |f a database access error occurs 8 getCo|umnD|sp|ayS|ze pub||c |nt getCo|umnD|sp|ayS|ze(|nt co|umn) throws SLLxcept|on Ind|cates the des|gnated co|umns norma| max|mum w|dth |n characters arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns the norma| max|mum number of characters a||owed as the w|dth of the des|gnated co|umn 1hrows SLLxcept|on |f a database access error occurs 9 getCo|umnLabe| pub||c Str|ng getCo|umnLabe|(|nt co|umn) throws SLLxcept|on Gets the des|gnated co|umns suggested t|t|e for use |n pr|ntouts and d|sp|ays arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns the suggested co|umn t|t|e 1hrows SLLxcept|on |f a database access error occurs 10 getCo|umnName pub||c Str|ng getCo|umnName(|nt co|umn) throws SLLxcept|on Get the des|gnated co|umns name arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns co|umn name 1hrows SLLxcept|on |f a database access error occurs 11getrec|s|on pub||c |nt getrec|s|on(|nt co|umn) throws SLLxcept|on ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
17
Get the des|gnated co|umns number of dec|ma| d|g|ts arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns prec|s|on 1hrows SLLxcept|on |f a database access error occurs 12get1ab|eName pub||c Str|ng get1ab|eName(|nt co|umn) throws SLLxcept|on Gets the des|gnated co|umns tab|e name arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns tab|e name or |f not app||cab|e 1hrows SLLxcept|on |f a database access error occurs 13getCo|umn1ype pub||c |nt getCo|umn1ype(|nt co|umn) throws SLLxcept|on ketr|eves the des|gnated co|umns SL type arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns SL type from [avasq|1ypes 1hrows SLLxcept|on |f a database access error occurs 14getCo|umn1ypeName pub||c Str|ng getCo|umn1ypeName(|nt co|umn) throws SLLxcept|on ketr|eves the des|gnated co|umns databasespec|f|c type name arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns type name used by the database If the co|umn type |s a userdef|ned type then a fu||yqua||f|ed type name |s returned 1hrows SLLxcept|on |f a database access error occurs 1S|skeadCn|y pub||c boo|ean |skeadCn|y(|nt co|umn) throws SLLxcept|on Ind|cates whether the des|gnated co|umn |s def|n|te|y not wr|tab|e arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns true |f so fa|se otherw|se 1hrows SLLxcept|on |f a database access error occurs 16|sWr|tab|e pub||c boo|ean |sWr|tab|e(|nt co|umn) throws SLLxcept|on Ind|cates whether |t |s poss|b|e for a wr|te on the des|gnated co|umn to succeed arameters co|umn the f|rst co|umn |s 1 the second |s 2 keturns true |f so fa|se otherw|se 1hrows SLLxcept|on |f a database access error occurs
Creat|ng a Ca||ab|eStatement Cb[ect Ca||ab|eStatement ob[ects are created w|th the Connect|on method prepareCa|| 1he examp|e be|ow creates an |nstance of Ca||ab|eStatement that conta|ns a ca|| to the stored procedure get1estData wh|ch has two arguments and no resu|t parameter Ca||ab|eStatement cstmt conprepareCa||( ca|| get1estData(? ?))
ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
18
Create a Database Table Using a Statement import java.sql.Connection; import java.sql.DataTruncation; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLWarning; import java.sql.Statement;
public class Main , public static void main(String, args) throws Exception , Connection conn = getConnection(); conn.setAutoCommit(false); Statement stmt = conn.createStatement();
stmt.executeUpdate("create table survey (id int, name CHAR(5) );");
Detect if a table exists import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet;
public class Main , public static void main(String, argv) throws Exception , Connection c = null; DatabaseMetaData dbm = c.getMetaData(); ResultSet rs = dbm.getTables(null, null, "employee", null); if (rs.next()) , System.out.println("Table exists"); , else , System.out.println("Table does not exist"); , , ,
Database Metadata 1. Metadata is data about data. 2. Database metadata is information about a database. 3. Database metadata provides information about the structure of a database and its tables, views, and stored procedures. JDBC provides four interfaces that deal with database metadata 1. java.sql.DatabaseMetaData: about the database as a whole: table names, table indexes, database product name and version, and actions the database supports. 2. java.sql.ResultSetMetaData: about the types and properties of the columns in a ResultSet object. 3. java.sql.ParameterMetaData: about the types and properties of the parameters in a PreparedStatement object. 4. javax.sql.RowSetMetaData: about the columns in a RowSet object. Get Table Names from a Database ComputerLducat|onCom 4u81!nv Colony 8lkaner h 0131223032394143033329414283934
ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); for (int i = 1; i <= numCols; i++) , if (i 1) System.out.print(", "); System.out.print(rsmd.getColumnLabel(i)); , System.out.println(""); while (rs.next()) , for (int i = 1; i <= numCols; i++) , if (i 1) System.out.print(", "); System.out.print(rs.getString(i)); , System.out.println(""); , conn.close(); ,