I successfully created an online MySQL database and connected my java program to it.
Now I wanted to get data out of it: In my database table "playerdata" are three rows:
ID, Username, Password
I created a JavaFX (changed from swing) window with one Textfield(for the username), a Passwordfield(for the password) and a button to login in.
This is my event code for the button:
//makes a query in table "Login" searching for "username"s and "password"s
String query = "select * from playerdata where username=? and password=?";
//passing query to preparedStt (statement)
PreparedStatement preparedStt = cnct.prepareStatement(query);
//setting Strings for username and password
preparedStt.setString(1, userName.getText()); //index 1 for username (upper ?)
preparedStt.setString(2, userPassword.getText()); //index 2 for username (upper ?) (index starts at 1)
ResultSet resSet = preparedStt.executeQuery(); //executes query result to resSet
int count = 0;
while(resSet.next()) {
count++; //increments counter
}
if(count == 1) { //goes one time if query matches user and pass
System.out.println("Access granted!");
} else { //otherwise login is incorrect
System.out.println("Access denied!");
System.exit(0);
}
//closes resources
resSet.close();
preparedStt.close();
So this query goes through all usernames and password and if they match with the Textfield(userName) and Passwordfield(userPassword) it grants access.
I encountered a problem! As I mentioned above I had three rows and this query searches for two rows. I'd like to know the ID of the player which logged in into my program. Do you have any idea how I could get this working?
I already tried making a third index (and id=?) and then use the resSet.getString(3), but it gives me an error which says that the index 3 isn't specified.
Thanks.