Adv Java Chp4 GSCH
Adv Java Chp4 GSCH
Adv Java Chp4 GSCH
4
ODBC
JDBC heavily influenced by ODBC
ODBC provides a C interface for database
Database
Type 3 Type 2 Native C/C++ API
Local API
Network API
Type 4
• Type 1: Uses a bridging technology to access a database. JDBC-
ODBC bridge is an example. It provides a gateway to the ODBC.
• Type 2: Native API drivers. Driver contains Java code that calls
native C/C++ methods provided by the database vendors.
• Type 3: Generic network API that is then translated into database-
specific access at the server level. The JDBC driver on the client uses
sockets to call a middleware application on the server that translates
the client requests into an API specific to the desired driver. Extremely
flexible.
• Type 4: Using network protocols built into the database engine talk
directly to the database using Java sockets. Almost always comes
only from database vendors.
Basic steps to use
a database in Java
1.Establisha connection
2.Create JDBC Statements
3.Execute SQL Statements
4.GET ResultSet
5.Close connections
7
1. Establish a connection
import java.sql.*;
Load the vendor specific driver
Class.forName("oracle.jdbc.driver.OracleDriver");
Whatdo you think this statement does, and how?
Dynamically loads a driver class, for Oracle database
9
Executing SQL Statements
String createLehigh = "Create table Lehigh
"+
"(SSN Integer not null, Name
VARCHAR(32), " + "Marks Integer)";
stmt.executeUpdate(createLehigh);
//What does this statement do?
ResultSet rs =
Stmt.executeQuery(queryLehigh);
//What does this statement do?
while (rs.next()) {
int ssn = rs.getInt("SSN");
String name = rs.getString("NAME");
int marks = rs.getInt("MARKS"); 11
Close connection
stmt.close();
con.close();
12