Пример #1
1
  /**
   * This method inserts one row for an entity table and gets latestPK of this row.
   *
   * @param query
   * @param variables
   * @param nullVars
   * @author ywang 11-26-2007
   */
  public void executeWithPK(String query, HashMap variables, HashMap nullVars) {
    clearSignals();

    Connection con = null;
    PreparedStatement ps = null;
    PreparedStatementFactory psf = new PreparedStatementFactory(variables, nullVars);
    try {
      con = ds.getConnection();
      if (con.isClosed()) {
        if (logger.isWarnEnabled()) logger.warn("Connection is closed: EntityDAO.execute!");
        throw new SQLException();
      }
      ps = con.prepareStatement(query);
      ps = psf.generate(ps); // enter variables here!
      if (ps.executeUpdate() != 1) {
        logger.warn("Problem with executing dynamic query, EntityDAO: " + query);
        throw new SQLException();

      } else {
        logger.info("Executing dynamic query, EntityDAO: " + query);

        if (getCurrentPKName == null) {
          this.latestPK = 0;
        }

        this.unsetTypeExpected();
        this.setTypeExpected(1, TypeNames.INT);

        ArrayList al = select(digester.getQuery(getCurrentPKName), con);

        if (al.size() > 0) {
          HashMap h = (HashMap) al.get(0);
          this.latestPK = ((Integer) h.get("key")).intValue();
        }
      }

    } catch (SQLException sqle) {
      signalFailure(sqle);
      if (logger.isWarnEnabled()) {
        logger.warn(
            "Exception while executing dynamic statement, EntityDAO.execute: "
                + query
                + ": "
                + sqle.getMessage());
        sqle.printStackTrace();
      }
    } finally {
      this.closeIfNecessary(con, ps);
    }
  }
Пример #2
0
  public ArrayList select(String query, HashMap variables) {
    clearSignals();

    ArrayList results = new ArrayList();
    ResultSet rs = null;
    Connection con = null;
    PreparedStatementFactory psf = new PreparedStatementFactory(variables);
    PreparedStatement ps = null;
    try {
      con = ds.getConnection();
      if (con.isClosed()) {
        if (logger.isWarnEnabled()) logger.warn("Connection is closed: GenericDAO.select!");
        throw new SQLException();
      }

      ps = con.prepareStatement(query);
      ps = psf.generate(ps); // enter variables here!
      rs = ps.executeQuery();
      if (logger.isInfoEnabled()) {
        logger.info("Executing dynamic query, EntityDAO.select:query " + query);
      }
      signalSuccess();
      results = this.processResultRows(rs);

    } catch (SQLException sqle) {
      signalFailure(sqle);
      if (logger.isWarnEnabled()) {
        logger.warn(
            "Exception while executing dynamic query, GenericDAO.select: "
                + query
                + ":message: "
                + sqle.getMessage());
        sqle.printStackTrace();
      }
    } finally {
      this.closeIfNecessary(con, rs, ps);
    }
    return results;
  }
Пример #3
0
  public void execute(String query, HashMap variables) {
    clearSignals();

    Connection con = null;
    PreparedStatement ps = null;
    PreparedStatementFactory psf = new PreparedStatementFactory(variables);
    try {
      con = ds.getConnection();
      if (con.isClosed()) {
        if (logger.isWarnEnabled()) logger.warn("Connection is closed: EntityDAO.execute!");
        throw new SQLException();
      }
      ps = con.prepareStatement(query);
      ps = psf.generate(ps); // enter variables here!
      if (ps.executeUpdate() < 0) { // change by jxu, delete can affect
        // more than one row
        logger.warn("Problem with executing dynamic query, EntityDAO: " + query);
        throw new SQLException();

      } else {
        signalSuccess();
        logger.info("Executing dynamic query, EntityDAO: " + query);
      }
    } catch (SQLException sqle) {
      signalFailure(sqle);
      if (logger.isWarnEnabled()) {
        logger.warn(
            "Exeception while executing dynamic statement, EntityDAO.execute: "
                + query
                + ": "
                + sqle.getMessage());
        sqle.printStackTrace();
      }
    } finally {
      this.closeIfNecessary(con, ps);
    }
  }