如何将一个变量保存到session中

rabiazhou 2017-08-04 10:48:36
求助如何将得到的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;
}
...全文
688 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmqw 2017-08-10
  • 打赏
  • 举报
回复
HttpServletRequest request servlet的dopost或doget的参数中 request.getSession.setAttribute("autoInckey",autoInckey)。
不能呼吸了 2017-08-10
  • 打赏
  • 举报
回复
这个方法返回值改成你要的数据,servlet调用这个方法,得到你要的数据 然后在servlet中 request.getSession.setAttribute("key",value)。
  • 打赏
  • 举报
回复
要给这个方法传递一个request对象

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧