如何将一个变量保存到session中
求助如何将得到的autoInckey在servlet层中保存到session中
public static boolean addUpdateDelete(String sql, Object[] arr) {
Connection con = null;
PreparedStatement ps = null;
int autoInckey = -1;
try {
con = BaseDao.getCon();//第一步:连接数据库的操作
ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);//第二步:预编译
//第三步:设置值
if(arr!= null && arr.length != 0) {
for(int i = 0; i < arr.length; i++) {
ps.setObject(i+1, arr[i]);
}
}
int count = ps.executeUpdate();//第四步:执行sql语句
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
autoInckey = rs.getInt(1);
System.out.println(autoInckey);
} else {
// throw an exception from here
}
if(count > 0) {
return true;
} else {
return false;
}
} catch (ClassNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}