Exemplo n.º 1
0
  public int save() {

    logger.debug("in dao save for " + this.getTable() + " " + this.getPKcol());

    int id = 0;
    SqlConn con = null;
    try {

      con = ConnFactory.getConnection(DB);

      if (existingRecord(con)) {
        id = saveExistingRecord(con);
        logger.debug("should have saved existing with id " + id);
      } else {
        id = saveNewRecord(con);
        logger.debug("should have saved new with id " + id);
      }

      if (getField(getPKcol()).getType() == DAOutils.INT_TYPE) setField(getPKcol(), id);

    } catch (Exception e) {
      logger.error(
          "error saving dao into "
              + table
              + " with id "
              + fields.get(PK).getValue()
              + " -- "
              + e.getMessage(),
          e);
      id = -1;
    } finally {
      if (con != null) con.close();
    }
    return id;
  }
Exemplo n.º 2
0
  public void delete() {
    SqlConn con = null;
    try {

      con = ConnFactory.getConnection(DB);
      con.prepareStatement("delete from " + table + " where " + PK + "=?");

      if (fields.get(PK).getType() == DAOutils.STRING_TYPE) con.setString(1, getValueAsString(PK));
      else con.setInt(1, getValueAsInt(PK));

      con.exPrep();

    } catch (SQLException e) {
      logger.error(
          "Failed to delete dao with id " + getField(PK).getValueAsInt() + " from table " + table,
          e);
    } finally {
      if (con != null) con.close();
    }
  }
Exemplo n.º 3
0
  public void load() {

    SqlConn con = null;
    logger.debug("dao load method");

    try {
      con = ConnFactory.getConnection(DB);
      String s = "select * from " + table + " where " + PK + "=:::";

      if (fields.get(PK).getType() == DAOutils.INT_TYPE)
        s = s.replace(":::", String.valueOf(fields.get(PK).getValueAsInt()));

      if (fields.get(PK).getType() == DAOutils.STRING_TYPE)
        s = s.replace(":::", "'" + fields.get(PK).getValueAsString() + "'");

      ResultSet rs = con.query(s);

      while (rs.next()) {
        for (Field field : fields.values()) {

          // for field names not in the DB that we want to use
          // programatically, we start the field name with "EMPTY_"
          if (field.name.startsWith("EMPTY_")) continue;

          logger.debug("setting field " + field.name + " with value " + rs.getObject(field.name));

          switch (field.getType()) {
            case DAOutils.STRING_TYPE:
              field.setValue(rs.getString(field.name));
              break;
            case DAOutils.INT_TYPE:
              field.setValue(-1);
              field.setValue(rs.getInt(field.name));
              break;
            case DAOutils.TIMESTAMP_TYPE:
              field.setValue(rs.getTimestamp(field.name));
              break;
            case DAOutils.FLOAT_TYPE:
              field.setValue(rs.getFloat(field.name));
              break;
            case DAOutils.DATE_TYPE:
              field.setValue(rs.getDate(field.name));
              break;
            case DAOutils.PASSWORD_TYPE:
              field.setValue(rs.getString(field.name));
              break;
            case DAOutils.BLOB_TYPE:
              field.setValue(rs.getBlob(field.name));
              break;
          }
        }
      }
    } catch (SQLException e) {
      logger.error(
          "failed to load object "
              + this.getClass().getName()
              + " from table "
              + table
              + " with id "
              + fields.get(PK).getValueAsString()
              + " -- "
              + e.getMessage(),
          e);
    } finally {
      if (con != null) con.close();
    }
  }