日期:2014-05-18  浏览次数:20858 次

关于PreparedStatement,在运行成功后为什么记录并没有插入到数据库中?
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();

代码如上所示,运行结果也显示了“注册成功!”,但在数据库中并没有看到添加的记录,这是怎么回事?

------解决方案--------------------
catch(Exception e)
{
out.println(e.getLocalizedMessage()); //看看是否报错了
}
------解决方案--------------------
还有你有没有使用事务啊?如果有还要提交事务啊!
------解决方案--------------------
对,看con是不是提交了?
还有就是刷新数据库看记录是否存在