Data Retrieval from a Remote Database
DATA RETRIEVAL FROM A REMOTE DATABASE
At the remote database a server listens for client connections. This server accepts SQL Queries from the client, exchanges it on the database and sends the response to the client.
PACKAGES INVLOVED:
!ava.lang.io , "ava.net and "ava.s#l.
CLASSES INVOLVED:-
Connection: A connection $session% &ith a specific database. SQL statements are executed and results are returned &ithin the co next of a connection.
Statement create Statement $% thro&s SQL 'xception, (reates a statement ob"ect for sending SQL statements to the database.
Driver Manager: The basic service for managing a set of !D)( drivers. As a part of its initiali*ation, the Driver +anager class &ill attempt to load the driver classes.
25
Data Retrieval from a Remote Database
,ublic static s-nchroni*ed connection get connection $String .rl, String user, String pass&ord% thro&s SQL 'xception. This method attempts to establish a connection to the given database .RL.
C a!!: /nstances of the class, class represent classes and interfaces in a running "ava application. 0nl- the "ava virtual +achine creates class ob"ects.
,ublic
static
(lass
for
1ame
$String
(lass
1ame%
thro&s
(lass1ot2ound'xception returns the class ob"ect associated &ith the class or interface &ith the given string name.
State"ent:
The ob"ect used for executing a static SQL statement and returning the results it produces
ResultSet executeQuer-$String s#l% thro&s SQL'xception executes the given SQL statement, &hich return a single Result Set ob"ect.
Re!# t Set :
26
Data Retrieval from a Remote Database
A table of data representing a database result set, &hich is usuall- generated bexecuting a statement that #ueries the database.
)oolean next$% thro&s SQL'xception +oves the cursor do&n one ro& from its current position. String getString$int (oloumn/ndex% thro&s SQL'xception. Retrives the values of the designed coloumn in the current ro& of this ResultSet ob"ect as a String. ResultSet+etaData get+etaData$% thro&s SQL'xception Retrieves the number, t-pes and properties of this ResultSet ob"ects coloumns.
Re!# tSetMetaData:
An ob"ect that can be used to get information about the t-pes and properties of the coloumns in a Resultset 0b"ect.
int get(oloumn(ourt$% thro&s SQL'xception Returns the number of coloumn in ResultSet 0b"ect. String get(oloumnLabel$int coloumn% thro&s SQL'xception 3ets the designated coloumns suggested tittle for use in displa-s.
27
Data Retrieval from a Remote Database
,rogram name ,ac4ages used 0b"ects used +ethods used (lasses used (onditional structures Loops used
: dbserver."ava : "ava.io.5,"ava.net.5,"ava.s#l.56 : con,rs,s,rm,ss,soc,br,p&,#r: main$% : D)Server : if : &hile, for
PROGRAM:
$% SERVER SIDE PROGRAM %$ 28
Data Retrieval from a Remote Database
import "ava.io.56 import "ava.net.56 import "ava.s#l.56 class rdbserver 7 public static void main$String args89% 7 (onnection conn:null6 Statement stmt:null6 ResultSet rs:null6 tr7 ServerSoc4et ssoc:ne& ServerSoc4et$;;;;%6 S-stem.out.println$<&ait for client connection:=n<%6
Soc4et csoc:ssoc.accept$%6 if$csoc>:null% S-stem.out.println$<client is connected:<%6
29
Data Retrieval from a Remote Database
)ufferedReader /nputStreamReader$csoc.get/nputStream$%%%6
fromc:ne&
)ufferedReader$ne&
,rintStream toc:ne& ,rintStream$csoc.get0utputStream$%%6 )ufferedReader /nputStreamReader$S-stem.in%%6 in:ne& )ufferedReader$ne&
String #uer-:<<6 String)uffer rset:null6 (lass.for1ame$<sun."dbc.odbc.!dbc0dbcDriver<%6
conn:Driver+anager.get(onnection$<"dbc:odbc:vinod<,<scott<,<tiger<%6 stmt:conn.createStatement$%6
do 7 #uer-:fromc.readLine$%6 S-stem.out.println$<client #uer- re#uest:<?#uer-%6 if$#uer-.e#uals/gnore(ase$<#uit<%% brea46
30
Data Retrieval from a Remote Database
rs:stmt.executeQuer-$#uer-%6
ResultSet+etaData rsmd:rs.get+etaData$%6
int no(ol:rsmd.get(olumn(ount$%6 if$rs.next$%% 7 rset:ne& String)uffer$<R'S.LT:=n<%6 for$int i:;6i@:no(ol6i??% rset.append$rsmd.get(olumnLabel$i%?<=n<%6 rset.append$<=n<%6 A do 7 for$int i:;6i@:no(ol6i??% rset.append$rs.getString$i%?<=t<%6 rset.append$<=n<%6 A &hile$rs.next$%%6
31
Data Retrieval from a Remote Database
rset.append$<<%6 toc.println$rset%6 A &hile$>#uer-.e#uals/gnore(ase$<#uit<%%6
conn.close$%6 A
catch$'xception e% 7 S-stem.out.println$e.get+essage$%%6 A A A
32
Data Retrieval from a Remote Database
$% CLIENT SIDE PROGRAM %$
PROGRAM:
import "ava.io.56 import "ava.net.56 import "ava.s#l.56 public class rdbclient 7 public static void main$String args89% 7
33
Data Retrieval from a Remote Database
S-stem.out.println$<connected to serverdata<%6 tr7 Soc4et soc:ne& Soc4et$<localhost<,;;;;%6 ,rintStream tos:ne& ,rintStream$soc.get0utputStream$%%6 )ufferedReader /nputStreamReader$soc.get/nputStream$%%%6 )ufferedReader /nputStreamReader$S-stem.in%%6 String #uer-:<<6 S-stem.out.println$<connected:=n<%6 S-stem.out.print$<enter #uer-:<%6 do 7 from4b:ne& )ufferedReader$ne& froms:ne& )ufferedReader$ne&
#uer-:from4b.readLine$%6 tos.println$#uer-%6 if$#uer-.e#uals/gnore(ase$<#uit<%% brea46 do
34
Data Retrieval from a Remote Database
7 #uer-:froms.readLine$%6 S-stem.out.println$#uer-%6 A &hile$>#uer-.startsBith$<<%%6 A &hile$>#uer-.e#uals/gnore(ase$<#uit<%%6 A catch$'xception e% 7 S-stem.out.println$e%6 A A A
35
Data Retrieval from a Remote Database
OUTPUT:
36