LIKE 的 JavaPreparedStatement SQL 语法

发布于 2024-10-06 17:31:20 字数 1170 浏览 0 评论 0原文

我有一个准备好的语句,如下所示:

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%';"

任何建议?

I have a prepared statement as so:

private static final String SQL_LIST_GROUP = "SELECT * 
                                                FROM table 
                                               WHERE group LIKE ?;"

My function, my function is as follow(shortened and SQL objects properly declared):

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);  
    }  
}

The conections are made properly, but I get a syntax error, the prepared statement with the value parsed is as follows:

"SELECT * 
   FROM table 
  WHERE group LIKE '%grp%';"

Any Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-10-13 17:31:20

我还在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.

浅紫色的梦幻 2024-10-13 17:31:20

我见过 DB 炸弹因为尾随 ;在动态SQL之前。删除它会导致它起作用吗?

I've seen a DB bomb out because of a trailing ; in dynamic SQL before. Does removing that cause it to work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文