0

I have a question related to Java & mySQL. I have a login and I want to extract some values from a row. This is how my table looks like : enter image description here

Once my login is successfull, I want to save into 2 variables the following : (My login works, I've just pasted the chunk of code where I have interest in)

The coins for the current logged int The privileges a user has in a String

In the main class I've declared the following

public static int coins;
public static String status;

This is the code :

      String sqlQuery = "SELECT * FROM username WHERE username = ? and password = MD5(?)";

      PreparedStatement pst = connect.prepareStatement( sqlQuery );
      pst.setString( 1, Proiect_final.username );
      pst.setString( 2, Proiect_final.pw );

    //  // Coins extraction !
//      String sqlQuery1 = "SELECT coins,status FROM username WHERE username = ?";
//      PreparedStatement pst1 = connect.prepareStatement(sqlQuery);
//      pst1.setString(1, Proiect_final.username);


ResultSet rs = pst.executeQuery();
if( rs.next() ) {

   coins = [?];

   System.out.println("You did it!");
   mainFrm abba = new mainFrm();

   abba.setVisible(true);


}

I'd appreciate any help.

1
  • coins = rs.getInt("coins"); Commented May 6, 2014 at 20:11

2 Answers 2

2

Consider always retrieving the necessary columns, not all the columns from a table. So, change the query to:

String sqlQuery = "SELECT coins, status FROM username WHERE username = ? and password = MD5(?)";

Then, execute it and retrieve "coins" column:

if (rs.next() ) {
    coins = rs.getInt("coins");
    status = rs.getString("status");
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

1

The methods you are looking for are

rs.getInt("coins");

and

rs.getString("status");

Alternatively, instead of using the catchall '*' in your select, you could specifically name those two columns and then retrieve them by column order. See the related API documentation here: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

Comments

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.