获取"java.sql.SQLException: Values not bound to statement"异常

8 浏览
0 Comments

获取"java.sql.SQLException: Values not bound to statement"异常

我试图制作一个程序,其中包含使用SQL连接登录系统的用户,然后如果凭据正确,用户将被重定向到另一个窗口。

但是,我遇到了一个问题,我想在SQL数据库中存储一些信息,所以我尝试使用while循环,它起作用了,但后来遇到了一个错误:

java.sql.SQLException: Values not bound to statement

请参考以下代码:

    String pseudo2 = null;
    String rank2 = null;
    try {
        String searchname2 = "select * from AdminsInfos where pseudo=?";
        PreparedStatement name2 = connection.prepareStatement(searchname2);
        ResultSet rspseudo2 = name2.executeQuery();;
        while (rspseudo2.next())
        {
            pseudo2 = rspseudo2.getString("Pseudo");
            rank2 = rspseudo2.getString("Rank");
        }
    } catch (Exception e2) {
        e2.printStackTrace();
    }
    JOptionPane.showMessageDialog(null, "用户名和密码正确,连接管理员!");
    frame.setVisible(false);
    new LoginMain().setVisible(true);
    LoginMain.usernameField.setText(pseudo2);
    LoginMain.ranklabel.setText("等级:" + rank2);

您也可以通过以下图片检查SQL数据库:

sql base

有人可以帮帮我吗?

0
0 Comments

原因:在执行语句之前,需要先设置绑定变量的值。如果绑定变量是字符串类型,可以使用setString方法来设置值。

解决方法:在执行查询语句之前,使用setString方法来设置绑定变量的值。如果值可能为null,需要特殊处理,可以参考stackoverflow上的解决方法。

0