Open Computing Institute, Inc.: 1 JDBC
Open Computing Institute, Inc.: 1 JDBC
• Design of JDBC
– based on ODBC and the
X/OPEN SQL Call Level Interface
– makes it easy for ODBC developers to learn
• JDBC-ODBC Bridge
– allows access to ODBC databases from Java
applications
• Two Tier
– Java applet communicates directly with a database
on the web server from which the applet was
downloaded
• requires a 100% Java database driver so it can be downloaded
– Java application communicates directly with a
database on any server
• JDBC driver doesn't have to be pure Java but must be on
the client
• Three Tier
– middle tier can provide
• a higher-level API, not just SQL
• control over database access
• performance advantages
– ex. load balancing and caching frequently accessed data
– Java applet communicates with a Java application on
the web server from which the applet was
downloaded (via sockets, RMI, or CORBA) which
communicates directly with a database on any server
– Java application communicates with a Java
application on any server (via sockets, RMI, or
CORBA) which communicates directly with a
database on any server
JDBC API
JDBC DriverManager
Java Applications
JDBC API
JDBC DriverManager
JDBC/ODBC Bridge
(a type of JDBC driver)
ODBC DriverManager
Database A B C Database D E F
• To delete a table
DROP TABLE table-name
import java.io.*;
import java.sql.*;
import sun.jdbc.odbc.*;
// Connect to a database.
// DriverManager.getConnection() checks each loaded driver
// in order and selects the first one that is able to
// process the specified database.
// AddressBookDB is associated with a specific database
// in Settings...Control Panel...32bit ODBC.
// No username or password is required for this database.
String url = "jdbc:odbc:AddressBookDB";
Connection con = name of data source,
DriverManager.getConnection(url, "", ""); not a database
String sql;
username password
ResultSet rs;
int rowCount;
• stmt.executeQuery(String sql)
– returns a ResultSet object
these were used in the
– used for these SQL statements previous example code
• SELECT
• stmt.executeUpdate(String sql)
– returns the number of rows affected
– used for these SQL statements
• INSERT - for inserting one row in a table
• UPDATE - for modifying one or more rows in a table
• DELETE - for deleting one or more rows in a table
• CREATE TABLE - returns zero
• DROP TABLE - returns zero
• ALTER TABLE - returns zero
• stmt.execute(String sql)
– for SQL statements that may be a query or an update
– used for stored procedures that result in
• more than one ResultSet
• more than one row count
• both ResultSets and row counts
– returns a boolean indicating whether results were
obtained
– use getMoreResults() and getResultSet() to get all
methods
in the ResultSets
Statement
class – use getUpdateCount() to get each row count
• returns -1 when there are no more
con.prepareCall("{call procedure-name}")