山东台男主持人名单:jsp中用session对象进行int型的储存,怎么老出错啊???急救!!!

来源:百度文库 编辑:中科新闻网 时间:2024/04/29 01:52:08
麻烦给我看一下
select * from tb_user where username='"+username+"' and status='用户' 执行语句结束后
进行如下的操作:
int rs_id=rs.getInt("id");
session.setAttribute("userid","rs_id");
session.setAttribute("username",username);
可为什么在别的页面进行session.getAttribute("userid");
时出现错误啊~~而session.getAttribute("username");却无任何错误?

其中表中字段id是int 型!

int userid=(Integer.parseInt(session.getAttribute("userid")));
int rs_id=rs.getInt("id");
session.setAttribute("userid","rs_id");

session.setAttribute("userid","rs_id");

我是这样使用的~~
结果出现javax.servlet.ServletException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]将 varchar 值 'null' 转换为数据类型为 int 的列时发生语法错误。

转化成String类型才行

这个我好像知道,你别用int,因为int是数据类型,不是类对象,你把它换作Integer,像类一样用。

Integer.parseInt函数会抛出异常,
你应当用try{}catch(Exception e){}捕获处理一下.如:
int userid;
try{
userid=(Integer.parseInt(session.getAttribute("userid")));
}catch(Exception e)
{userid=0;
}