0

I am creating a simple login application in java netbeans. I have made a connection with a MS-Access database in which i am storing the usernames and their passwords.

Suppose if I want to get text from a JTextField, i would use an object of PrepareStatement for the query. For Example 'ps' is the object of PrepareStatement, now to get text from JTextField i write, ps.setString(1, jTextField1.getText());

As JPasswordField returns a character array, how can i get password from the JPasswordField using PrepareStatement object 'ps' ?

Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • 1
    And hint: there is a reason that Swing has a special JPasswordField, and that this thing is returning a char[] and not a string. If you are serious about what you are doing, you really have to study and **understand** such details; see http://stackoverflow.com/questions/983964/why-does-jpasswordfield-getpassword-create-a-string-with-the-password-in-it for example. – GhostCat Nov 14 '16 at 20:50
  • yeah i know why it returns a character array, that's why i asked if there was another way of getting password from JPasswordField without having to convert it in String like the one suggested below. – Yousaf Nov 14 '16 at 20:57
  • 1
    It's designed this way specifically for security reasons. The alternative is creating your own prepared statement implementation that overloads setString to take a char[] array. – Compass Nov 14 '16 at 20:59

1 Answers1

2

Just create a new String from the char[].

ps.setString(1, new String(jPasswordField.getPassword()));
Kayaman
  • 72,141
  • 5
  • 83
  • 121