I am trying to make a query that takes all the values of a column, which can be username or email (using the columnName variable). Initially I had a normal query, but after reading more I found out that I should do a PreparedStament. However, the preparedStatement.setString(1, columnName) call it doesn't seem to work considering I have a for loop below and it doesn't display anything.
public class EmailOrUsernameAuthenticityVerifier {
public EmailOrUsernameAuthenticityVerifier(
String profileEmailOrUsername, String columnName)
throws SQLException, ClassNotFoundException,
EmailOrUsernameAlreadyExistException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name",
"username", "password");
PreparedStatement preparedStatement =
conn.prepareStatement("SELECT ? FROM profile");
preparedStatement.setString(1, columnName);
ResultSet rs = preparedStatement.executeQuery();
Set<String> columnNameSet = new HashSet<>();
ResultSetMetaData rsmd = rs.getMetaData();
int columnNumber = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnNumber; i++) {
String columnValue = rs.getString(i);
columnNameSet.add(columnValue);
}
}
// This for is just for testing
for (String s: columnNameSet) {
System.out.println(s);
}
if (columnNameSet.contains(profileEmailOrUsername)) {
throw new EmailOrUsernameAlreadyExistException(
profileEmailOrUsername);
}
}
}