I want to validate whether the telephone number exists in the database column(TelephoneNumber
). If exists I will return true
or else false
This code is not only to verify the TelephoneNumber
Column, It may validate other DB columns like FirstName
, LastName
, EmailAddress
etc
public boolean executeDBQuery(String tableName, String columnName,
String columnValue) {
try {
PreparedStatement ps = null;
String query = "SELECT TOP 1 " + columnName + "FROM" + tableName
+ "WHERE" + columnName + "=" + '?';
ps = conn.prepareStatement(query);
ps.setString(1, columnValue);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return true;
} else {
return false;
}
} catch (SQLException ex) {
}
return false;
}
Main Method:
public static void main(String[] args) {
Database db = new Database();
boolean result = db.executeDBQuery("Application","FirstName","asd");
System.out.println(result);
}
The above code is returning a true value even if the values are not found in the DB. Not sure what I'm doing wrong here. Can anyone help me out