-2

I have 3 questions regarding my code below used for user registration.

1) How can I prevent users from registering themselves providing empty value in the username and password area?

2) I wonder if it's a good practice to both dispose & run the form again in case user credentials already exist in database. Is there any other way to go back to the main screen?

3) My programs runs correctly but there is a warning message in a line like "The method getText() from the type JPasswordField is deprecated". What does that mean exactly?

Code:

public void actionPerformed(ActionEvent arg0) {


            try
                    {
              String userID = userIDinput.getText();
              String pass = passwordField.getText();
              String myDriver = "com.mysql.jdbc.Driver"; //jdbc driver loaded
              String myUrl = "jdbc:mysql://localhost:3306/masterdata_db?autoReconnect=true&useSSL=false";
              Class.forName(myDriver);
              Connection conn = DriverManager.getConnection(myUrl, "root", "");

              Statement st = conn.createStatement();
              ResultSet resultSet;

              String check = "SELECT Username FROM userregistration WHERE Username = '"+userID+"' ";      
              resultSet = st.executeQuery(check);


              if(!resultSet.next()){

                  String sql = "INSERT INTO userregistration"
                            + "(Username, Password) VALUES"
                            + "(?,?)";
                  PreparedStatement pSt=conn.prepareStatement(sql);
                  pSt.setString(1, userID);
                  pSt.setString(2, pass);     
                  pSt.executeUpdate();   
                  conn.close();

              }

              else {

                  JOptionPane.showMessageDialog(frame, "USER ALREADY EXISTS. TRY ANOTHER ONE!", "Inane error",JOptionPane.ERROR_MESSAGE);
                  dispose();
                  new Login().setVisible(true);
              }


              }
            catch (Exception e)
                {

                  e.printStackTrace();
                }

            }
Shadow
  • 33,525
  • 10
  • 51
  • 64
BlockeeR
  • 221
  • 1
  • 4
  • 16
  • deprecated methods mean they should not be used anymore and are purely there for compatibility reasons, if you writing new code avoid deprecated methods completely. Instead use getPassword() method instead of getText() – A.A Dec 07 '17 at 20:37
  • Pls do not ask more than 1 question per post. Makes answering and searching a lot more difficult! – Shadow Dec 07 '17 at 20:42

1 Answers1

0

1)

if(userID.trim().isEmpty())//trim removes whitespaces 
{
    throw new SomeKindOfException();
    //or just ...
    return; 
}

See this question.

2) Just remove the dispose and new Login(). JOptionPane will automatically "return to your main screen".

3) You should use password-fields like this:

JPasswordField passwordField = new JPasswordField();
char[] chars = passwordField.getPassword();
String pwd = new String(chars);
for (int i=0; i<chars.length; i++) chars[i] = ’x’; //overwrite password when done for more security

See How to Use Password Fields for more information.

The JPasswordField uses char[] instead of String, because Strings are immutable. For more information see this question.

Sir ExecLP
  • 83
  • 1
  • 5