/**
* 保存PO,保存后,返回自动增长的id,返回-1为保存失败,返回-2为保存成功但未获得id
* @param entity
* @param tablename
* @return true 成功,false 失败
* @throws SQLException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int saveGetId(Object entity, String tablename) throws SQLException,
IllegalArgumentException, IllegalAccessException {
int id = -1;
//.....操作
String sql = "insert into " + tablename + " (" + ...+ ") values (" +... + ")";
int count = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);//必须使用这个,带有两个参数的方法
if(count > 0){
id = -2;
if(res.next()) {
id = res.getInt(1);
}
}
SqlBean.closeConnection(con, stmt, rs); // 关闭连接
return id;
}
* 保存PO,保存后,返回自动增长的id,返回-1为保存失败,返回-2为保存成功但未获得id
* @param entity
* @param tablename
* @return true 成功,false 失败
* @throws SQLException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int saveGetId(Object entity, String tablename) throws SQLException,
IllegalArgumentException, IllegalAccessException {
int id = -1;
//.....操作
String sql = "insert into " + tablename + " (" + ...+ ") values (" +... + ")";
int count = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);//必须使用这个,带有两个参数的方法
if(count > 0){
id = -2;
if(res.next()) {
id = res.getInt(1);
}
}
SqlBean.closeConnection(con, stmt, rs); // 关闭连接
return id;
}