JDBC by Jon Pearce
Entity-Relation Diagram
Person
OID
FIRST
LAST
ADDRESS
PHONE
Phone
OID
AREA
NUMBER
Address
OID
STREET
CITY
STATE
1
*
*
1
java.sql
Connection
createStatement()commit()rollback()
<<Interface>>
java.xml
DriverManager
getDriver()Driver
<<Interface>>
connect()
<<creates>>
<<creates>>
Statement<<Interface>>
executeQuery()
<<creates>>
ResultSet<<Interface>>
<<creates>>
JDBC Driver Types
• JDBC-to-ODBC Bridge
• Native API partly Java drivers
• JDBC-Net pure Java drivers
• Native-protocol pure Java drivers
An SQL Browser
Console
controlLoop()help()about()handle()execute()
Connection<<Interface>>
Statement<<Interface>>
<<creates>>
SQLBrowser
execute()
ResultSet<<Interface>>
<<creates>>
Reusable CUI Class
SQL Browser Declaration
public class SQLBrowser extends Console { protected String driverName; protected String dbaseName; protected Connection connection; protected Statement statement; protected ResultSet result; public SQLBrowser(String db) throws SQLException, ClassNotFoundException {...} public void finalize() throws SQLException {...} private String toString (ResultSet rs) throws SQLException {...} public String execute(String sql) throws AppError {...}}
Connecting
public SQLBrowser(String db) throws SQLException, ClassNotFoundException { Class.forName(driverName); dbaseName = "jdbc:cloudscape:rmi:" + db; connection = DriverManager.getConnection(dbaseName); statement = connection.createStatement(); meta = connection.getMetaData();}
Executing a Query
public String execute(String sql) throws AppError { String answer = "???"; try { result = statement.executeQuery(sql); answer = toString(result); } catch (SQLException e) { } return answer;}
Processing a Result Set
private String toString (ResultSet rs) throws SQLException { StringBuffer results = new StringBuffer(); ResultSetMetaData metaData = rs.getMetaData(); int numCols = metaData.getColumnCount(); for(int i = 1; i <= numCols; i++) { // get column names results.append(metaData.getColumnName(i) + "\t"); } results.append("\n"); while(rs.next()) { // get next row for(int i = 1; i <= numCols; i++) { results.append(rs.getObject(i) + "\t"); } results.append("\n"); } return results.toString();}
Closing the Connection
public void finalize() throws SQLException { statement.close(); connection.close();}
DBase DAO
public class DBaseDAO { protected String driver; protected String url; // "protocol:subprotocol:dbase" protected Connection connection; public DBaseDAO(String d, String u) throws DAOException {...} protected void connect() throws Exception {...} public void close() throws DAOException {...} protected void finalize() {...}}
Connecting
protected void connect() throws Exception { Class.forName( driver ); connection = DriverManager.getConnection( url ); connection.setAutoCommit( false );}
DAO Pattern
PersonDAO<<Interface>>
insert()delete()find()update()
DAOFactory<<Interface>>
makeDAOFactory()makePersonDAO()
CloudscapeDAOFactgory
makePersonDAO()
<<creates>>
DBasePersonDAO
<<creates>>
uses JDBC