0

I have this method to check whether the email has already existed in my MySQL database. I am using the client-server model. This code here will says that the email existed no matter how I try entering new emails/fields.

public static String doAddUser(String firstName, String lastName, String email, String password, String retypedPassword) throws SQLException {
        String returnStatement = "";

        Connection connection = null;
        connection = establishConnection();
        final String queryCheck = "SELECT * from usersdata WHERE email = ?";
        final PreparedStatement ps = connection.prepareStatement(queryCheck);
        ps.setString(1, email);
        final ResultSet resultSet = ps.executeQuery();
}

However, this code here will reset my connection immediately. I know that string concatenation in MySQL is a bad practice but nevertheless, this does not work.

public static String doAddUser(String firstName, String lastName, String email, String password, String retypedPassword) throws SQLException {
    String returnStatement = "";

    Connection connection = null;
    connection = establishConnection();
    final String queryCheck = "SELECT * from usersdata WHERE email = '" + email + "'";
    final PreparedStatement ps = connection.prepareStatement(queryCheck);
        ps.setString(1, email);
        final ResultSet resultSet = ps.executeQuery();
}

Is there a way to work around this problem? I am stuck because I have 5 fields while I only need to validate the existence of one field only (email).

kyrie3
  • 35
  • 5

0 Answers0