package foo; import javax.naming.*; import javax.sql.*; import java.sql.*; public final class TestData { public TestData() { init(); } public void init() { try{ Context ctx = new InitialContext(); if(ctx == null ) throw new Exception("Boom - No Context"); ds = (DataSource)ctx.lookup( "java:comp/env/jdbc/testdb"); } catch (Exception e) { System.out.println("testdata ds lookup exception: " + e); } } /** * Return the foo. */ public String getFoo() { return (this.foo); } /** * Set the foo. */ public void setFoo(String foo) { this.foo = foo; } /** * Return the bar. */ public String getBar() { return (this.bar); } /** * Set the bar. */ public void setBar(String bar) { this.bar = bar; } public void load() { ResultSet rs = null; Statement stmt = null; Connection conn = null; try { if (ds != null) { conn = ds.getConnection(); if(conn != null) { foo = "Got Connection "+conn.toString(); stmt = conn.createStatement(); rs = stmt.executeQuery("select id, foo, bar from testdata"); if(rs.next()) { foo = rs.getString("foo"); bar = rs.getString("bar"); } rs = null; stmt = null; conn.close(); } } } catch(Exception e) { System.out.println("testdata exception: " + e); e.printStackTrace(); } finally { //this should be checked to see if connections is actually not closed //enclose this in a finally block to make //sure the connection is closed try { if (conn != null) conn.close(); } catch (SQLException e) { System.out.println("testdata finally: " + e); System.out.println("e: " + e); //getServlet().log("Connection.close", e); } } } // --------------------------------------------------- Instance Variables private String foo = null; private String bar = null; private DataSource ds = null; }