예제 #1
0
  public int updateByObject(Leave leave) throws Exception {
    StringBuffer sql = new StringBuffer();
    Array array = new Array();
    List<String> fields = new ArrayList<String>();
    sql.append("update t_leave set ");
    int result = -1;
    try {
      if (leave.getDate() != null) {
        fields.add("date");
        array.add(leave.getDate());
      }
      if (leave.getMark() != null) {
        fields.add("mark");
        array.add(leave.getMark());
      }
      if (leave.getHours() > 0) {
        fields.add("hours");
        array.add(leave.getHours());
      }
      if (leave.getReason() != null) {
        fields.add("reason");
        array.add(leave.getReason());
      }

      for (int i = 0; i < fields.size(); i++) {
        if (i == 0) {
          sql.append(fields.get(i)).append(" = ?");
        } else {
          sql.append(",").append(fields.get(i)).append(" = ?");
        }
      }

      if (leave.getId() < 0) {
        return -1;
      } else {
        sql.append(" where id = ?");
        array.add(leave.getId());
      }

      Object[] params = array.getArray();
      result = jdbc.update(sql.toString(), params);
    } catch (Exception e) {
      throw e;
    }

    return result;
  }
예제 #2
0
  public int addObjectReturnId(Connection conn, Leave bean) throws Exception {
    StringBuffer sql = new StringBuffer();
    sql.append("insert into t_leave (");
    sql.append("date,hours,reason,mark");
    sql.append(") values (");
    sql.append("?,?,?,?");
    sql.append(")");

    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      DatabaseMetaData dmd = conn.getMetaData();
      ps = conn.prepareStatement(sql.toString(), new String[] {"id"});
      ps.setDate(1, DateUtils.formatToSqlDate(bean.getDate()));
      ps.setDouble(2, bean.getHours());
      ps.setString(3, bean.getReason());
      ps.setString(4, bean.getMark());

      ps.execute();
      if (dmd.supportsGetGeneratedKeys()) {
        rs = ps.getGeneratedKeys();
        while (rs.next()) {
          bean.setId(rs.getInt(1));
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }

    return bean.getId();
  }
예제 #3
0
 @Override
 public Leave mapRow(ResultSet rs, int index) throws SQLException {
   Leave leave = new Leave();
   leave.setDate(rs.getDate("date"));
   leave.setHours(rs.getDouble("hours"));
   leave.setId(rs.getInt("id"));
   leave.setMark(rs.getString("mark"));
   leave.setReason(rs.getString("reason"));
   return leave;
 }
예제 #4
0
  public int addObject(Leave leave) throws Exception {
    StringBuffer sql = new StringBuffer();
    StringBuffer values = new StringBuffer();
    String title = "insert into t_leave (";
    sql.append(title);
    Array array = new Array();
    List<String> fields = new ArrayList<String>();
    int result = -1;
    try {
      if (leave.getDate() != null) {
        fields.add("date");
        array.add(leave.getDate());
      }
      if (leave.getMark() != null) {
        fields.add("mark");
        array.add(leave.getMark());
      }
      if (leave.getHours() > 0) {
        fields.add("hours");
        array.add(leave.getHours());
      }
      if (leave.getReason() != null) {
        fields.add("reason");
        array.add(leave.getReason());
      }
      for (int i = 0; i < fields.size(); i++) {
        if (i == 0) {
          sql.append(fields.get(i));
          values.append("?");
        } else {
          sql.append(",").append(fields.get(i));
          values.append(",?");
        }
      }
      sql.append(") value (").append(values.toString()).append(")");
      result = jdbc.update(sql.toString(), array.getArray());
    } catch (Exception e) {
      throw e;
    }

    return result;
  }