my java dekstop send error like this what can i do?
getText() in JPasswordField has been deprecated
i have tried to replace getText()
and change to getPassword()
:
pst.setString(2,txtPass.getPassword());
my java dekstop send error like this what can i do?
getText() in JPasswordField has been deprecated
i have tried to replace getText()
and change to getPassword()
:
pst.setString(2,txtPass.getPassword());
Convert the char[]
to String
. For that matter, you can simply use the String
constructor String(char[] value)
:
pst.setString(2, new String(txtPass.getPassword()));
Alternatively, you can simply use @SuppressWarnings("deprecation")
, as the main point of the deprecation, avoiding to create an immutable String
, is entirely defeated by constructing a String
out of the char[]
array afterwards.
However, this is not a good idea, getText()
is deprecated for a reason:
Once you've created the String, if another process can dump memory, there's no way (aside from reflection) you can get rid of the data before garbage collection kicks in.
With an array, you can explicitly wipe the data after you're done with it. You can overwrite the array with anything you like, and the password won't be present anywhere in the system, even before garbage collection.