1

I was trying to make a program which consists of connecting an user by a login system with SQL, then if the credentials are good the user is redirected to an another frame.

But I had a problem, I want to have some information in the SQL base, so I have tried to use while loop and it was working, but after I encountered an error :

java.sql.SQLException: Values not bound to statement

See the following code :

    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, "Username and password are correct, connection Admin !");

    frame.setVisible(false);
    new LoginMain().setVisible(true);

    LoginMain.usernameField.setText(pseudo2);
    LoginMain.ranklabel.setText("Rank : " + rank2);

and you can check the SQL base too by the following picture :

sql base

Can someone help me?

1
  • You have a ? placeholder in your SQL statement. It must be set prior to executeQuery using a PreparedStatement method such as setString, setInt, or likewise depending on the data type. Commented May 5, 2016 at 18:48

1 Answer 1

2

Since you have a bound variable you need to set the value before executing the statement.

for example , if psuedo is of type String then you will be doing something like below.

    String searchname2 = "select * from AdminsInfos where pseudo=?";
    PreparedStatement name2 = connection.prepareStatement(searchname2);
    name2.setString(1,"value");

    ResultSet rspseudo2 = name2.executeQuery();

where first parameter in the setString means you want to set the first value for the bound variable.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, The pseudo is exactly null at beginning, and after it is setted by my SQL
OOOH I SEE ! The value can be null and after it will be corrected by the SQL with a set or like ?
No it wont. if the value is null then your query will look for rows where value for psuedo column is "null" string. to avoid this issue you have to do something like this. stackoverflow.com/questions/4215135/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.