LIKE 的 JavaPreparedStatement SQL 语法
我有一个准备好的语句,如下所示:
private static final String SQL_LIST_GROUP = "SELECT *
FROM table
WHERE group LIKE ?;"
我的函数,我的函数如下(缩短并正确声明 SQL 对象):
public List< MyType > getGroupList(Long grp) {
Connection link = null;
PreparedStatement query = null;
ResultSet rstList = null;
List< MyType > list = new ArrayList< MyType >();
try {
link = MySQL.getConnection();
link.setAutoCommit(false);
query = link.prepareStatement(SQL_LIST_GROUP);
query.setString(1, "%"+grp.toString()+",%");
rstList = query.executeQuery();
link.commit();
while (rstList.next()) {
list.add(MapFields(rstList));
}
return list;
} catch (SQLException e) {
throw new DAOException(e);
} finally {
close(link, query, rstList);
}
}
连接正确,但出现语法错误,解析值的准备好的语句如下:
"SELECT *
FROM table
WHERE group LIKE '%grp%';"
任何建议?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还在PreparedStatemts 中看到了分号的问题。
此外,该方法的参数称为“grp”,但在调用 setString 时使用“group”。这是问题的一部分吗? group.toString() 应该足够了,不需要 valueOf/longValue 的东西。
另一件事是,“group”是 MySQL 中的保留字。
I have also seen issues with semi-colons in PreparedStatemts.
Also, the parameter to the method is called 'grp', but 'group' is being used in the call to setString. Is that a type-o, or part of the problem? group.toString() should suffice, no need for the valueOf/longValue stuff.
Another thing, 'group' is a reserved word in MySQL.
我见过 DB 炸弹因为尾随 ;在动态SQL之前。删除它会导致它起作用吗?
I've seen a DB bomb out because of a trailing ; in dynamic SQL before. Does removing that cause it to work?