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?

ZentsuGo
  • 146
  • 1
  • 2
  • 6
  • 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. – rgettman May 05 '16 at 18:48

1 Answers1

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.

Deendayal Garg
  • 5,030
  • 2
  • 19
  • 33
  • Well, The pseudo is exactly null at beginning, and after it is setted by my SQL – ZentsuGo May 05 '16 at 19:13
  • OOOH I SEE ! The value can be null and after it will be corrected by the SQL with a set or like ? – ZentsuGo May 05 '16 at 19:14
  • 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. http://stackoverflow.com/questions/4215135/how-to-deal-with-maybe-null-values-in-a-preparedstatement – Deendayal Garg May 05 '16 at 19:18