Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用like运算符在数据库中搜索并返回结果列表_Mysql_Jdbc_Sql Like - Fatal编程技术网

Mysql 使用like运算符在数据库中搜索并返回结果列表

Mysql 使用like运算符在数据库中搜索并返回结果列表,mysql,jdbc,sql-like,Mysql,Jdbc,Sql Like,以下是我搜索特定字符串的方法: (我想在书名表中搜索) 但当我调用此方法时: 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:1

以下是我搜索特定字符串的方法:

(我想在书名表中搜索)

但当我调用此方法时:

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 + "%");
编辑Re,其他问题

AFAIKJava没有任何功能,因此需要更改方法签名。 目前,您正在返回第一个结果,然后再也不返回函数

我的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 List searchInDB(字符串关键字){
列出字符串=新列表();
String url=“jdbc:mysql://localhost/bookstore";
String query=“从图书中选择书名,书名如%?%”;
试一试{
Connection Connection=DriverManager.getConnection(…);
PreparedStatement ps=connection.prepareStatement(查询);
ps.setString(1,关键字);
结果集rs=ps.executeQuery();
while(rs.next()){
Add(rs.getString(“title”);
}
}捕获(SQLException sqle){
printStackTrace();
}
返回字符串;
}

我相信您的意思是说
如果(结果!=null)
?我尝试
如果(结果!=null)
但它只返回一个值,但DBMS结果中有几个值。@user3808021我已更新-您需要返回一组字符串
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 ;
}