I'm trying to retrieve some data from an SQL table using JDBC through the BufferedReader
, the code I wrote for this execution:
System.out.println("Type a name");
String nname = br.readLine();
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/VetTest", "root", "root");
Statement stmt=(Statement) con.createStatement();
String query = "select * from Pet WHERE name LIKE '?%'";
ResultSet rs=(ResultSet) stmt.executeQuery(query);
while(rs.next())
System.out.println(rs.getString("name"));
if (petn.equals(query)) {
System.out.println("Searching names.." + nname + query);
}
I don't know if I'm doing it wrong, so to summarize my question, I'm trying to retrieve some data depend about what the user inputs in the console. E.g. I'm trying to search for the name Jack in my database I want my application to search for this person's name or a similar person name.
The result I always get when I enter a petname (even though the pet the name is available in my database):
No such a name