package edu.mit.lcs; import java.sql.*; import javax.sql.*; import javax.naming.*; import java.util.*; public class SqlAccess { public String dbName; Context env = null; DataSource source = null; Connection con = null; Statement stmt = null; ResultSet rs = null; public SqlAccess() throws NamingException,SQLException { this("jdbc/admin"); } public SqlAccess(String dbName) throws NamingException,SQLException { //System.out.println("Creating SqlAccess with db=" + dbName); this.dbName = dbName; env = (Context) new InitialContext().lookup("java:comp/env"); //System.out.println("Got env java:comp/env = " + env.toString()); source = (DataSource) env.lookup(dbName); //System.out.println("Got datasource=" + source.toString()); con = source.getConnection(); //System.out.println("Got Connection=" + con.toString()); //stmt = con.createStatement(); //System.out.println("Got Statement=" + stmt.toString()); } public void close() { try { if (stmt!=null) { stmt.close(); stmt = null; } if (con!=null) { con.close(); con = null; } } catch (SQLException e) { System.err.println("Error closing SqlAccess: " + e); } } /** * retrieve retrieves a single row from a SQL relation, as * specified by the sql argument. * If nothing matched the query or an error occurs, the result is * null. */ public Entity retrieve(String sql) { try { stmt = con.createStatement(); rs = stmt.executeQuery(sql); return (rs.next()) ? new Entity(rs) : null; } catch (SQLException e) { return null; } finally { if (rs!=null) { try { rs.close(); rs = null; } catch (Exception e) {} } if (stmt!=null) { try { stmt.close(); stmt = null; } catch (Exception e) {} } } } public ArrayList retrieveAll (String sql) { try { stmt = con.createStatement(); ArrayList a = new ArrayList(); rs = stmt.executeQuery(sql); while (rs.next()) { a.add(new Entity(rs)); } return a; } catch (SQLException e) { return null; } finally { if (rs!=null) { try { rs.close(); rs = null; } catch (Exception e) {} } if (stmt!=null) { try { stmt.close(); stmt = null; } catch (Exception e) {} } } } /** Retrieves metadata for the current database. * for mysql, five columns are retrieved for each table: * TABLE_TYPE is always TABLE * TABLE_SCHEM is null * TABLE_CAT is the name of the database * REMARKS are typically null * TABLE_NAME the name of the table. */ public ArrayList retrieveMeta () { try { stmt = con.createStatement(); DatabaseMetaData meta = con.getMetaData(); ArrayList a = new ArrayList(); rs = meta.getTables(null,null,null, new String [] {"TABLE"}); while (rs.next()) { a.add(new Entity(rs)); } return a; } catch (SQLException e) { return null; } finally { if (rs!=null) { try { rs.close(); rs = null; } catch (Exception e) {} } if (stmt!=null) { try { stmt.close(); stmt = null; } catch (Exception e) {} } } } /** Retrieves metadata about a particular table in the database. */ public SqlTable getSqlTable (String tableName) { try { stmt = con.createStatement(); rs = stmt.executeQuery("select * from " + tableName); ResultSetMetaData rsmd = rs.getMetaData(); int nCol = rsmd.getColumnCount(); String [] colNames = new String[nCol]; int [] colTypes = new int[nCol]; String [] colTypeNames = new String[nCol]; int [] colLengths = new int[nCol]; int [] colPrec = new int[nCol]; for (int i = 0; i