All pastes #451106 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-04-21 07:59 UTC
rendered paste body
  private static Connection con = null;

  private static Connection getConnection() throws Exception {
    // get context: provides the starting point for resolution of names
    Context ctx = new InitialContext();
    if (ctx == null) {
      throw new Exception("No Context");
    }
    // retrieve datasource
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/xxDB");
    if (ds == null) {
      throw new Exception("No Datasource");
    }
    // return db connection
    return ds.getConnection();
  }

  public static void doSomething() {
    Statement stmt = null;
    try {
      // get connection
      con = getConnection();
      if (con == null) {
        throw new Exception("No Connection");
      }
      stmt = con.createStatement();
      stmt.executeUpdate("UPDATE yy SET `zz`=0 WHERE `ww`='oo'");
    }
    catch (Exception e1) {
      //
    }
    // close resources
    finally {
      try {
        stmt.close();
        stmt = null;
      }
      catch (SQLException e2) {
        //
      }
      try {
        con.close();
        con = null;
      }
      catch (SQLException e3) {
        //
      }
    }
  }