Download - LETS LEARN JDBC
-
7/25/2019 LETS LEARN JDBC
1/13
JDBCLETS UNDERSTAND IT!
14-1
-
7/25/2019 LETS LEARN JDBC
2/13
14-2
JDBC
JDBCis an alternative to ODBC and ADO that
provides database access to programs written in
Java.
JDBC is not an acronm ! it doesn"t stand #oranthing$
% Do o& believe that$' (-)
JDBC drivers are available #or most DB*+prod&cts,
% http,ava.s&n.comprod&ctsdbc
http://java.sun.com/products/jdbc/http://java.sun.com/products/jdbc/ -
7/25/2019 LETS LEARN JDBC
3/13
14-/
JDBC Driver 0pes
-
7/25/2019 LETS LEARN JDBC
4/13
14-4
Java +ervlets and Applets
Java programs are complied into an operating sstem independent
bytecode.
ario&s operating sstems &se their own bytecode interpreters
aa Java virtual acines.
An applet is transmitted to a browser via 300 and is invoed onthe client worstation &sing the 300 protocol.
A servletis a Java program that is invoed on the server to respond
to 300 re5&ests.
0pe / and 0pe 4 drivers can be &sed #or both applets and
servlets. 0pe 2 drivers can be &sed onl in servlets.
-
7/25/2019 LETS LEARN JDBC
5/13
14-6
JDBC Components
-
7/25/2019 LETS LEARN JDBC
6/13
Database rogramming +teps
1. 7stablish a connection2. Begin transaction
/. Create a statement obect
4. Associate +89 with the statement obect6. rovide val&es #or statement parameters
:. 7;ec&te the statement obect
. ?elease reso&rces14-:
-
7/25/2019 LETS LEARN JDBC
7/13
@sing JDBC
1a. 9oad the driver,% 0he driver class libraries need to be in the C9A++A03 #or the
Java compiler and #or the Java virt&al machine.
% 0he most reliable wa to load the driver into the program is,
Class.forName(string).newInstance();1b. 7stablish a connection to the database,
% A connection @?9 string incl&des the literal dbc, #ollowed b thename o# the driver and a @?9 to the database
String url =
"jdbc:oracle:thin:reddwarf.cs.rit.edu:!#!:csodb"; jdbc $sub%rotocol&$subname& host %ort database
% Create a Connection obect,
Connection con = 'rieranager.getConnection(url*
db+ser* db,assword);
14-();
>. ?elease reso&rcescon.close();
14->
-
7/25/2019 LETS LEARN JDBC
10/13
14-1
@sing a repared+tatement ?? 0nce 7ou hae a connection* 7ou can create a ?? "%re%ared statement" object. / %re%ared statement is ?? %recom%iled* and can be used re%eatedl7 with new alues ?? for its %arameters. ?? +se 6uestion mar>s for %arameter %lace@holders. ,re%aredStatement %re%Stmt = con.%re%areStatement(
"INS2- IN-0 /rtist (/rtistI'* Name* "8 "Nationalit7* 5irth'ate* 'eceased'ate)"8 "A/3+2S (/rtistSe6.ne
-
7/25/2019 LETS LEARN JDBC
11/13
14-11
Accessing a stored proced&re ?? 0nce 7ou hae a connection* 7ou can create a ?? "callable statement" object to access the stored %rocedure. ?? Inside the curl7 braces* 7ou call the %rocedure* and use ?? 6uestion mar>s for %arameter %lace@holders. CallableStatement callStmt = con.%re%areCall(
"Hcall ecord1sale( B* B* B* B* B* B)" );
?? Now su%%l7 alues for the %arameters ?? ,arameters are referenced in order starting with !. callStmt.setString( !* "5arr7 oldwater" ); callStmt.setString( #* "-obe7" );
callStmt.setString( 9* "7stic Dabric" ); callStmt.setString( E* "!?!9" ); callStmt.setInt ( * #E );
?? /nd register the 0+- ariable ?? -his ariable returns information to this %rogram ?? from the stored %rocedure. callStmt.register0ut,arameter( J* jaa.s6l.-7%es.A/CK/ ); S7stem.out.%rintln( ",arameters set for CallableStatement" );
?? -he CallableStatement object has an additional methodL ?? for use when the stored %rocedure uses multi%le S3 statements. ?? !) e
-
7/25/2019 LETS LEARN JDBC
12/13
@sing ADO.70
1. 7stablish a connection to the database,String connection1string =
"Serer=(local);+I'=sa;,M'=;'/-/5/S2=%ubs;Connection -imeout=J";H0dbc0le'bS3Connection con =
new H0dbc0le'bS3Connection( connection1string );
Con.0%en();
2. Begin the transactionH0dbc0le'bS3-ransaction trans = con.5egin-ransaction(
OIsolation3eel.HChaoseadCommittedead+ncommitted
e%eatableeadSerialiPable+ns%ecifiedQ );
/. Create a statement Command) obectH0dbc0le'bS3Command statement = con.CreateCommand();
statement.-ransaction = trans;
14-12
-
7/25/2019 LETS LEARN JDBC
13/13
@sing ADO.70 cont.)
4. Associate +89 with the statement Command) obectstatement.Command-e