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:
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
,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
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
,rogram name ,ac4ages used 0b"ects used +ethods used (lasses used (onditional structures Loops used
PROGRAM:
$% SERVER SIDE PROGRAM %$ 28
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
)ufferedReader /nputStreamReader$csoc.get/nputStream$%%%6
fromc:ne&
)ufferedReader$ne&
conn:Driver+anager.get(onnection$<"dbc:odbc:vinod<,<scott<,<tiger<%6 stmt:conn.createStatement$%6
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
conn.close$%6 A
catch$'xception e% 7 S-stem.out.println$e.get+essage$%%6 A A A
32
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
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&
35
OUTPUT:
36