// Use case 1.
String authorName = "Wei She";
String query = "SELECT * FROM articles WHERE authors LIKE %?%";
PreparedStatement getAuthors = newConn.prepareStatement(query);
getAuthors.setString(1, authorName);
ResultSet resultSet = getAuthors.executeQuery();
while (resultSet.next()) {
String authors = resultSet.getString("authors");
System.out.println(authors);
}
这是我的代码的一个子集。数据已经在我本地的 MySQL 数据库中了。其余的连接代码存在。
我的问题是:我应该如何格式化 Java 代码中的 %?% 部分?有什么我想念的吗?这是错误输出。
Exception in thread "main" java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/%'Wei She'/%' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:975)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1025)
at src.TestSQLQuery.main(TestSQLQuery.java:34)
我在 MySQL Workbench 中使用了一个类似的查询:SELECT * FROM 文章 WHEREauthors LIKE '%Wei She%';
上述查询在 Workbench 中运行良好,并返回与其他作者以及“Wei She”一起的多个条目。
牛魔王的故事
相关分类