1
if (searchTerm.contains(".com") && searchTerm.contains("@")) {
    System.out.println("In email check");
    psmt.setString(1, "EMAIL_ID");
    psmt.setString(2, searchTerm);
} else if (numresult==true){
    System.out.println("In number check");
    psmt.setString(1, "MOBILE_NUMBER");
    psmt.setString(2, searchTerm);
} else if (uresult == true | alphanumeic == true) {
    System.out.println("In username check");
    psmt.setString(1, "USER_NAME");
    psmt.setString(2, searchTerm);
}

rs = psmt.executeQuery();

while(rs.next()) {
    HomeVo vo = new HomeVo();
    vo.setId(rs.getInt("ID"));
    vo.setFirstName(rs.getString("FIRST_NAME"));
    vo.setLastName(rs.getString("LAST_NAME"));
    vo.setEmailId(rs.getString("EMAIL_ID"));
    vo.setNumber(rs.getString("MOBILE_NUMBER"));
    vo.setQualification(rs.getString("QUALIFICATION"));
    vo.setState(rs.getString("STATE"));
    vo.setGender(rs.getString("GENDER"));
    vo.setUserName(rs.getString("USER_NAME"));
    vo.setDob(rs.getString("DOB"));
    return vo;
}

Here is my MySQL query.

String FETCH_USER_BY_SEARCHTERM = "SELECT * FROM SignUpTable WHERE ? = ? ";

Based on the search term given by the user, this logic checks if it is a username, email or mobile number. And feeds that as the input parameter. The control goes to the particular correct if statement, but won't return any values.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I am trying to update the column name dynamically and it is taking as SELECT * FROM SignUpTable WHERE 'Email_Id' = 'searchTerm'. Any way I can make it SELECT * FROM SignUpTable WHERE Email_Id = 'searchTerm'. – Shashank B A Nov 20 '22 at 18:32
  • it is technically possible, but I think it's bad practice. Checkout this question and [answer](https://stackoverflow.com/a/1208477/1514647) – luckyluke Nov 20 '22 at 19:11

1 Answers1

0

I think there is probably some problem with the statement. You should log the prepared statement before executing it:

System.out.println(psmt);
rs = psmt.executeQuery();

Then you should check the SQL in the MySQL console, if it returns any results. Alternatively you can check the query log. See here how to enable it.

luckyluke
  • 491
  • 7
  • 16