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(); }
@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; }