Search code examples
javasqlpasswordsjpasswordfield

How to add a password to a prepared statement in java


Can any one help with this issue that I'm having using prepared statements. The problem comes when trying to add the password to the statement which I am taking from a JPasswordField.

String Query = "SELECT * FROM Users WHERE Username = ? AND Password = ?";

PreparedStatement PrepedStatement = Con.prepareStatement(Query);

PrepedStatement.setString(1, Username.getText());
PrepedStatement.setString(2, Password.getPassword());
//It is no having any of this as getPassword() returns a Char[] which isn't a string

So does anyone have any ideas. Cheers.


Solution

  • This will likely work, however, I strongly consider noting Elliott Frisch's comment.

    String has an overloaded constructor can take a character array as a parameter.

    PrepedStatement.setString(2, new String(Password.getPassword()));