Reputation: 19
Im trying to do a Statement where the user would be prompted for the authors name and then all of the authors values would be output, below works but doesn't output anything.
public static void WhichAuthorQry (Connection conn)
{
Scanner sc = new Scanner(System.in);
try {
Statement stmt = (Statement) conn.createStatement();
System.out.println("Please enter the Authors name to search:");
String name = sc.next();
String SQL = "SELECT * FROM Poet WHERE PoetName='" + name + "'";
ResultSet rs = stmt.executeQuery(SQL);
while(rs.next()) {
System.out.println("Name: " + rs.getString("PoetName"));
System.out.println("Language: " + rs.getString("Language"));
}
}catch (Exception e){
System.out.println("ERROR: " + e.getMessage());
}
}
Upvotes: 0
Views: 53
Reputation: 458
I recommend to put your query into a prepared statement and offer the user to use wildcards:
PreparedStatement ps = conn.prepareStatement("SELECT * FROM Poet WHERE PoetName like ?");
ps.setString(1,name);
ResultSet rs = ps.executeQuery() ;
while ( rs.next() ) {
System.out.println("Name: " + rs.getString("PoetName"));
System.out.println("Language: " + rs.getString("Language"));
}
Upvotes: 2
Reputation: 311
Quickest thing I would think of is, does the case of name you enter, match the PoetName?
You could try
String SQL = "SELECT * FROM Poet WHERE lower(PoetName)= lower('" + name + "')";
Upvotes: 0