Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPasswordField problem

Status
Not open for further replies.

JTan

Technical User
Oct 9, 2001
73
SG
Hi all,

I am wondering how should I retrieve the password which the user inputs to enter the program?

My interface consists of a JPasswordField. I have tried to use the following codes but it only returns me absurd string when I was doing testing.

Pls kindly advise.

String results = (String)rs.getObject(1);

if (jPasswordField.getPassword().toString().compareTo(results) == 0 )
{
hide();
Main main = new Main();
main.show();
}else {
txtresults.setText(jPasswordField.getPassword().toString());
//txtresults.setText("Incorrect User ID/password! Please try again.");
}
 
That's because the getPassword() method returns a char[], not a String. When you do a toString() on this array, you're getting an object reference converted to a String, hence the weird output.

Try this:-
Code:
if (new String(jPasswordField.getPassword()).compareTo(results) == 0 )
It's not tidy, but it should get you moving again...

Tim
 
...also, what's going on with your first line? Is rs a ResultSet? If so, do this:-
Code:
String results = rs.getString(1);
instead.

As a parting shot, there are security implications with handling the passwords as Strings (the getText() methods of JPasswordField have been deprecated because of this). I'm assuming this is to do the JVM caching the Strings and that the correct method is to use array comparison instead, maybe with the java.util.Arrays.equals() method.

Can anyone elaborate on this issue, maybe?

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top