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();
}
}