Esempio n. 1
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();
  }
Esempio n. 2
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;
  }
Esempio n. 3
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;
  }