I have an assignment for a database management systems class that requires me to create an SQL database for students at a university and populate the database with random data. I have to create a Java program that allows users to query the database and I keep getting the error "Error connecting to database: Unknown column 'firstName' in 'where clause'" and I'm not sure how to fix it. This is the java code I am running:
private static void searchByName(Connection conn, Scanner input) throws SQLException {
System.out.print("Enter search string: ");
String search = input.nextLine();
String query = "SELECT * FROM Students WHERE LOWER(firstName) LIKE ? OR LOWER(lastName) LIKE ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, "%" + search.toLowerCase() + "%");
stmt.setString(2, "%" + search.toLowerCase() + "%");
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String firstName = rs.getString("firstName");
String lastName = rs.getString("lastName");
String id = rs.getString("id");
System.out.println(firstName + " " + lastName + " (" + id + ")");
}
}
}
}
The expected output is supposed to show the name of the student that the user inputted in the command line.