JDBC Notes PDF
JDBC Notes PDF
Connectivity (JDBC)
J2EE application model
J2EE is a multitiered distributed application model
client machines
the J2EE server machine
the database or legacy machines at the back end
JDBC API
JDBC is an interface which allows Java code to
execute SQL statements inside relational
databases
Java JDBC
driver
program
connectivity for Oracle
data processing
utilities driver
for MySQL
jdbc-odbc ODBC
bridge driver
The JDBC-ODBC Bridge
Step 2) Specify the name and location of the database being used
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 Diagram
SQL data
e.g. jdbc:pointbase://localhost/myDB
Statement Object
Cursor operations:
first(), last(), next(), previous(), etc.
cursor
Typical code: 23 John
while( rs.next() ) { 5 Mark
// process the row; 17 Paul
}
98 Peter
Accessing a ResultSet (Contd.)
meta data
ID Name Course Mark
007 James Bond Shooting 99
008 Aj. Andrew Kung Fu 1
Accessing Meta Data
Servlet
Client 1 …… Client n
JDBC in J2EE
Step 1: Start Sun Application Server PE 8
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.io.*;
import java.util.*;
try
{
PrintWriter pw = res.getWriter();
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");
pw.println("name = " + name + " Value = " + val + " Date = " + date);
}
Example: JDBC Using JNDI & Connection Pools (Contd.)
stmt.close();
}
catch(SQLException ex2)
{
System.out.println(ex2);
}
catch(IOException ex3)
{
System.out.println(ex3);
}
catch(Exception ex4)
{
System.out.println(ex4);
}
}
}
Reference
Database and Enterprise Web Application Development in J2EE,
Xiachuan Yi, Computer Science Department, University of Georgia.