Java Q&A
by Tim Kientzle

Example 1: 

java.sql.Connection db = null;
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
try {
    // Obtain database connection
    // prepare statement
    // execute statement
    // Retrieve results
} catch(java.sql.SQLException e) {
  System.err.println(/* error message */);
  e.printStackTrace();
  throw e;
} finally {
  if(rs!=null)
     try { rs.close(); }
     catch(java.sql.SQLException e) {}
  if(stmt!=null)
     try { stmt.close(); }
     catch(java.sql.SQLException e) {}
  if(db!=null) { /* Release connection */ }
}

Example 2: 

(a)
SELECT user_id FROM user_table WHERE user_name='bob';
SELECT cart_item, product_id FROM shopping_cart WHERE user_id=77;
SELECT product_name FROM products WHERE product_id=231;
SELECT product_name FROM products WHERE product_id=527;
SELECT product_name FROM products WHERE product_id=1099;

(b)
SELECT u.user_id, c.cart_item, p.product_name
  FROM user_table as u, shopping_cart as c, products as p
  WHERE u.user_id=c.user_id
     AND c.product_id=p.product_id;


1

