Professional Documents
Culture Documents
JDBC Workshop Solutions
JDBC Workshop Solutions
Programming
Solutions to
Workshops
Java JDBC Programming: Solutions to Workshops Page i
Table of Contents
2. Connecting to Database
import java.sql.*;
--------------------------------------------------------
}
while( rsCusts.next() ) {
Customer cust = new Customer();
cust.cust_no = rsCusts.getInt("cust_no");
cust.lastname = rsCusts.getString("lastname");
cust.firstname = rsCusts.getString("firstname");
cust.street = rsCusts.getString("street");
cust.city = rsCusts.getString("city");
cust.state = rsCusts.getString("state");
cust.zip = rsCusts.getString("zip");
cust.area_code = rsCusts.getString("area_code");
cust.phone = rsCusts.getString("phone");
v.add(cust);
}
return v;
}
if(bEditable) {
Statement stmt = _conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Test updateable RS
ResultSet rs = stmt.executeQuery(strSQL);
return rs;
}
else {
Statement stmt = _conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
return rs;
}
}
--------------------------------------------------------
public void updateRow(ResultSet rs, Object[] objData,
String[] strColNames) throws SQLException {
rs.moveToInsertRow();
for( int i = 0; i < objData.length; i++ ) {
if(objData[i] != null)
rs.updateObject(strColNames[i], objData[i]);
}
rs.insertRow();
rs.moveToCurrentRow();
}
--------------------------------------------------------
return true;
}
--------------------------------------------------------
public void disconnect() throws SQLException {
if(_conn != null && ! _conn.isClosed()) {
_conn.close();
_conn = null;
}
_prepOrders = null;
_prepItems = null;
}
--------------------------------------------------------
public Vector getOrders(Customer cust) throws SQLException {
Vector v = new Vector();
_prepOrders.setInt(1, cust.cust_no);
ResultSet rsOrders = _prepOrders.executeQuery();
while( rsOrders.next() ) {
Order ord = new Order();
ord.cust_no = cust.cust_no;
ord.order_no = rsOrders.getInt("order_no");
ord.city = rsOrders.getString("city");
ord.state = rsOrders.getString("state");
ord.order_price = rsOrders.getFloat("order_price");
ord.order_date = rsOrders.getDate("order_date");
v.add(ord);
}
rsOrders.close();
return v;
}
--------------------------------------------------------
public Vector getItems(Order ord) throws SQLException {
_prepItems.setInt(1, ord.order_no);
ResultSet rsItems = _prepItems.executeQuery();
while( rsItems.next() ) {
Item item = new Item();
item.order_no = ord.order_no;
item.item_no = rsItems.getInt("item_no");
item.product_id = rsItems.getInt("product_id");
item.quantity = rsItems.getInt("quantity");
item.price = rsItems.getFloat("price");
v.add(item);
}
return v;
}
7. Getting Metadata
_prepOrders = null;
_prepItems = null;
_dbmd = null;
}
--------------------------------------------------------
public Vector getSchemas() throws SQLException {
Vector v = new Vector();
rsSchemas.close();
return v;
}
--------------------------------------------------------
public Vector getTables(String strSchemaName)
throws SQLException {
ResultSet rsTables =
_dbmd.getTables(null, strSchemaName, null, null);
while( rsTables.next() )
v.add( rsTables.getString( 3 ));
rsTables.close();
return v;
}
--------------------------------------------------------
public Vector getColumns(String strTableName)
throws SQLException {
ResultSet rsColumns =
_dbmd.getColumns(null, null, strTableName, null);
while( rsColumns.next() )
v.add( rsColumns.getString( 4 ) + "\n " +
rsColumns.getString( 6 ));
rsColumns.close();
return v;
}
--------------------------------------------------------
public String[] getGeneralMetadata() throws SQLException {
String strInfo[] = new String[4];
strInfo[0] = _dbmd.getDatabaseProductName();
strInfo[1] = _dbmd.getDatabaseProductVersion();
strInfo[2] = _dbmd.getDriverName();
strInfo[3] = _dbmd.getDriverVersion();
return strInfo;
}
return true;
}
--------------------------------------------------------
public void sumCustomerOrders(OrderTally ot)
throws SQLException {
_csProc.setInt(1, ot.customer.cust_no);
_csProc.setFloat(2, ot.min_amount);
_csProc.executeUpdate();
ot.tally = _csProc.getFloat(2);
ot.order_count = _csProc.getInt(3);
}