关于PreparedStatement,在运行成功后为什么记录并没有插入到数据库中?

wushengyu 2007-08-16 05:28:59
String str="select * from tb_user_lt where username='"+username+"'";
ResultSet rs=stmt.executeQuery(str);
if(!rs.next())
{
try
{
//通过预处理对象向数据库中添加记录
PreparedStatement pstmt=con.prepareStatement("insert into tb_user_lt(username,password,nicheng) values(?,?,?)");
pstmt.setString(1,username);
pstmt.setString(2,password1);
pstmt.setString(3,nicheng);
pstmt.executeUpdate();
//String str1="insert into tb_user_lt(username,password,nicheng) values";
//str1=str1+"("+username+","+password1+"'"+nicheng+")";
//ResultSet rs1=stmt.executeQuery(str1);
}
catch(Exception e)
{
e.printStackTrace();
}
out.println("<script language='javascript'>alert('注册成功!');parent.location.href='login.html';</script>");
rs.close();
stmt.close();
con.close();

代码如上所示,运行结果也显示了“注册成功!”,但在数据库中并没有看到添加的记录,这是怎么回事?
...全文
541 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wushengyu 2007-08-30
  • 打赏
  • 举报
回复
谢谢,不过我的问题还是没有解决,我没有用到事务,代码是在书本上抄下来的,刚开始学习JSP,我想经过努力该问题会解决的!
ABCatai 2007-08-17
  • 打赏
  • 举报
回复
对,看con是不是提交了?
还有就是刷新数据库看记录是否存在
bengan 2007-08-17
  • 打赏
  • 举报
回复
还有你有没有使用事务啊?如果有还要提交事务啊!
孟子E章 2007-08-16
  • 打赏
  • 举报
回复
catch(Exception e)
{
out.println(e.getLocalizedMessage()); //看看是否报错了
}

81,115

社区成员

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

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