使用 like 运算符和 return 结果列表在数据库中搜索
Search in database with like operator and return list of results
这是我搜索特定字符串的方法:
(我想在书名表中搜索)
private static String searchInDB(String keyword) {
String url = "jdbc:mysql://localhost/bookstore";
String query = "Select title from books where title like %?% ";
try {
Connection connection = DriverManager.getConnection(...);
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, keyword);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
return rs.getString("title");
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return null;
}
但是当我调用这个方法时:
System.out.println(searchInDB("so"));
结果出现异常:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which
is 0).
null
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
...
更新
我添加此代码以获得所有结果,但我进入了一个具有相同值的无限循环!
String result = searchInDB("so");
while (result != null) {
System.out.println(result);
}
更改代码,使通配符包含在参数中,而不是在查询中,即:
String query = "Select title from books where title like ?";
....
ps.setString(1, "%" + keyword + "%");
编辑回复,其他问题
AFAIK Java 没有 yield return 功能,因此您需要更改方法签名。
目前,您正在返回第一个结果,然后永远不会返回到函数。
我的 Java 很基础,但是怎么样:
private static List<String> searchInDB(String keyword) {
List<String> theStrings = new List<String>();
String url = "jdbc:mysql://localhost/bookstore";
String query = "Select title from books where title like %?% ";
try {
Connection connection = DriverManager.getConnection(...);
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, keyword);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
theStrings.Add(rs.getString("title"));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return theStrings ;
}
这是我搜索特定字符串的方法:
(我想在书名表中搜索)
private static String searchInDB(String keyword) {
String url = "jdbc:mysql://localhost/bookstore";
String query = "Select title from books where title like %?% ";
try {
Connection connection = DriverManager.getConnection(...);
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, keyword);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
return rs.getString("title");
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return null;
}
但是当我调用这个方法时:
System.out.println(searchInDB("so"));
结果出现异常:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which
is 0).
null
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
...
更新
我添加此代码以获得所有结果,但我进入了一个具有相同值的无限循环!
String result = searchInDB("so");
while (result != null) {
System.out.println(result);
}
更改代码,使通配符包含在参数中,而不是在查询中,即:
String query = "Select title from books where title like ?";
....
ps.setString(1, "%" + keyword + "%");
编辑回复,其他问题
AFAIK Java 没有 yield return 功能,因此您需要更改方法签名。 目前,您正在返回第一个结果,然后永远不会返回到函数。
我的 Java 很基础,但是怎么样:
private static List<String> searchInDB(String keyword) {
List<String> theStrings = new List<String>();
String url = "jdbc:mysql://localhost/bookstore";
String query = "Select title from books where title like %?% ";
try {
Connection connection = DriverManager.getConnection(...);
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, keyword);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
theStrings.Add(rs.getString("title"));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return theStrings ;
}