























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























^ client machines^ ^ the J2EE server machine^ ^ the database or legacy machines at the back end
The JDBC-ODBC Bridge (Contd.) ^ The layers of translation (Java --> C -->SQL) can slow down execution.
JDBC Pseudo Code All JDBC programs do the following: Step 1) load the JDBC driver Step 2) Specify the name and location of the database being used Step 3) Connect to the database with a
Connection
object
^ Step 4) Execute a SQL query using a
Statement
object
^ Step 5) Get the results in a
ResultSet
object
^ Step 6) Finish by closing the
ResultSet
,^ Statement
and^ Connection
objects
^ Set up a database server (Oracle , MySQL, pointbase) ^ Get a JDBC driver^
set CLASSPATH for driver lib^ ^
Set classpath in windows, control panel->system->advanced->environment variable Set classpath in Solaris, set CLASSPATH to driver jar file ^ Import the library^
import java.sql.*; ^ Specify the URL to database server^
String
url = "jdbc:pointbase://127.0.0.1/test“ ^ Load the JDBC driver^
Class.forName("
com.pointbase.jdbc.jdbcUniversalDriver
");
^ Connect to database server^
Connection con = DriverManager.getConnection(url, “dbUser", “dbPass"); ^ Create SQL Statement^
stmt = con.createStatement(); ^ Execute SQL^
stmt.executeUpdate("insert into COFFEES " + "values('Colombian', 00101, 7.99, 0,0)"); ResultSet rs = stmt.executeQuery(query); JDBC API in J2SE
Statement stmt = con.createStatement();String sql= "select * from Traps";ResultSet rs = stmt.executeQuery(sql);String name;double val;java.sql.Date date;while (rs.next()){
name = rs.getString("TrapName");val = rs.getDouble("TrapValue");date = rs.getDate("TrapDate");System.out.println("name = " + name + " Value = " + val + " Date = " + date); } stmt.close();con.close();} catch(ClassNotFoundException ex1){
System.out.println(ex1); } catch(SQLException ex2){
System.out.println(ex2); } } JDBC Example (Contd.) }
JDBC Diagram DriverManager
Connection
Statement
ResultSet
creates
creates
creates Driver SQL SQL
data data
make linkto driver
Specify the URL to database server ^ The name and location of the database isgiven as a URL^
Database URLjdbc:pointbase: //host.domain.com: 9092
/data/file
The commsprotocol
The machineholding thedatabase.
The path tothe databaseon the machine
jdbc:pointbase://localhost/myDB
Databaseport
ResultSet Object ^ Stores the results of a SQL query. ^ A
first()
while( rs.next() ) {// process the row;}
JohnMarkPaulPeter
cursor
Accessing a ResultSet
getString(), getDate(), getInt(),getFloat(), getObject() Accessing a ResultSet (Contd.)
Meta Data^
^ e.g. the number of columns, the types ofthe columns^ ^ meta data is the
Name
Course
Mark
James Bond
Shooting
Aj. Andrew
Kung Fu
meta data