Programming Interaction. JDBC JDBC Operations Performed by atabase Engine
!ost databases handles "#$%S&l 'uery in four steps
Parse the incoming &uery.
$ompile the S'( &uery.
Optimi)e the data path.
E*ecute the optimi)ed &uery to ac&uire and return
data
In "#$+ Statement interface and it,s sub interface known
as PreparedStatement interface are used to &uery the database. import -a.a.s&l./0 public class "#$E*ample 1 public static .oid main2String34 args5 1 $onnection conn 6 null0 Statement stmt 6 null0 String s&l6770 try1 %%S8EP 9: ;egister "#$ dri.er $lass.for<ame2=com.mys&l.-dbc.ri.er=50 conn 6 ri.er!anager.get$onnection2>-dbc:mys&l:%%localhost:??@A%mys&l7+ USE;+ PASS50 stmt 6 conn.createStatement250 s&l 6 =I<SE;8 I<8O ;egistration BA(UES 2C@@+ ,Beer,+CD5=0 stmt.e*ecuteUpdate2s&l50 s&l 6 =I<SE;8 I<8O ;egistration BA(UES 2C@C+ ,Eara,+ CF5=0 stmt.e*ecuteUpdate2s&l50 System.out.println2=Inserted records into the table...=50 Gcatch2S'(E*ception se51 %%Handle errors for "#$ se.printStack8race250 Gcatch2E*ception e51 %%Handle errors for $lass.for<ame e.printStack8race250 G G Statement Interface Example Static S'( statement Static S'( statement import -a.a.s&l./0 public class "#$E*ample 1 public static .oid main2String34 args5 1 $onnection conn 6 null0 Statement stmt 6 null0 String s&l6770 try1 %%S8EP 9: ;egister "#$ dri.er $lass.for<ame2=com.mys&l.-dbc.ri.er=50 conn 6 ri.er!anager.get$onnection2>-dbc:mys&l:%%localhost:??@A%mys&l7+ USE;+ PASS50 stmt 6 conn.createStatement250 s&l 6 =I<SE;8 I<8O ;egistration BA(UES 2C@@+ ,Beer,+CD5=0 stmt.e*ecuteUpdate2s&l50 s&l 6 =I<SE;8 I<8O ;egistration BA(UES 2C@C+ ,Eara,+ CF5=0 stmt.e*ecuteUpdate2s&l50 System.out.println2=Inserted records into the table...=50 Gcatch2S'(E*ception se51 %%Handle errors for "#$ se.printStack8race250 Gcatch2E*ception e51 %%Handle errors for $lass.for<ame e.printStack8race250 G G Statement Interface Example Static S'( statement Static S'( statement Iorking of Statement Interface
(et us understand how Statement interface is
working I<SE;8 I<8O ;egistration BA(UES 100, 'Veer', 19 JDBC I<SE;8 I<8O ;egistration BA(UES 101, 'Zara', 18 It is clear now for e.ery new statement database has to do parsing+compiling and e*ecuting operations always. Parse $ompile E*ecute Problem with Statement interface
atabase parses the same &uery multiple
times+e*ecutes and fetches the result.
All four steps that is
parsing+compiling+optimi)ing+fetching are always repeated.
<etwork traffic to the atabase is hea.y since
same &uery goes multiple times.
8o o.ercome this problem we ha.e to use
precompiled statements. JDBC PreparedStatement
It is inherited from Statement interface.
In database management systems+ a prepared statement or
parameteri)ed statement is a feature used to e*ecute the same or similar database statements repeatedly with high efficiency
It preJe*ecutes parsing+compiling+optimi)ing. 8hus+ when creating
PreparedStatement some optimi)ation is done immediately.
8he statement template is created by the application and sent to the
database management system 2#!S5. $ertain .alues are left unspecified+ called parameters+ placeholders or bind .ariables 2labelled =K= below5: JDBC insert into Employee .alues2K+K50 S'( 8E!P(A8E PlaceholderC Placeholder9 & ' JDBC PreparedStatement Working I<SE;8 I<8O ;egistration BA(UES2K+K+K5 100, 'Veer', 20 101, 'Zara', 19 Parse $ompile E*ecute As we could see that PreparedStatement is parsed and compiled only once and e*ecute many times using new .alues. TALENTSPRINT | Copyrigt !"#! $ir%t Second JDBC PreparedStatement Interface PreparedStatement ps = cn.PrepareStatement (UPDATE emp SET eName= ? WHERE empno = ?); S&pplying 'al&e% for parameter% ps.setStrn!("#Tom); ps.set$nt(%#"&"); (al&e of te $ir%t Parameter (al&e of te Second Parameter $ir%t Parameter Second Parameter TALENTSPRINT | Copyrigt !"#! JDBC JDBC JDBC R&le% to remem)er R&le% to Remem)er Same Re%<Set o)*ect %o&ld not )e &%ed again once it i% terminated )y +ile loop ;esultSet rs6st.e*ecute'uery2>Select / from emp750 while2rs.ne*t255 1 System.out.println2rs.getInt2>empno7550 G ;esultSet rsC6st.e*ecute'uery2>Select / from emp750 while2rsC.ne*t255 1 System.out.println2rs.getInt2>empno7550 G 8he >rs7 ob-ect is used again which will cause >operation not allowed again7 TALENTSPRINT | Copyrigt !"#! R&le% to Remem)er ,&t &%ing %ame Re%<Set reference +e can deri'e many re%<% at different place% in te program ;esultSet rs6st.e*ecute'uery2>Select / from emp750 while2rs.ne*t255 1 System.out.println2rs.getInt2>empno7550 G rs6st.e*ecute'uery2>Select / from emp750 while2rs.ne*t255 1 System.out.println2rs.getInt2>empno7550 G 8his time >rs7 will retrie.e result %( ' "#$