【问题标题】:Unable to execute LIKE query in HSQLDB无法在 HSQLDB 中执行 LIKE 查询
【发布时间】:2013-05-24 12:37:05
【问题描述】:

我需要执行 LIKE 查询。我创建如下:

AData ad : ao.find(AData.class, Query.select().where("user=? AND ISSUES LIKE %?%",user,issueId)))      //issues field contain values as //"FIN-1,FIN-2,FIN7". issueId parameter has value as FIN-7

它给了我以下错误:

java.sql.SQLException: Unexpected token: % in statement [SELECT * FROM PUBLIC.AO_0371A8_ADATA WHERE    user=? AND ISSUES LIKE %?%]

将下面的查询更新为:

AData ad : ao.find(AData.class, Query.select().where("user=? AND ISSUES LIKE '%? %'",user,issueId)))  //issues field contain values as //"FIN-1,FIN-2,FIN7". issueId parameter has 

值为 FIN-7

它给了我以下错误:(参数索引超出范围:2)(也尝试为“.......LIKE \'%?%\'”......但给我与以下相同的错误:

com.atlassian.activeobjects.internal.ActiveObjectsSqlException: There was a SQL exception thrown  by the Active Objects library:
 Database:
- name:HSQL Database Engine
- version:1.8.0
- minor version:8
- major version:1
 Driver:
- name:HSQL Database Engine Driver
- version:1.8.0

java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 2
    at    com.atlassian.activeobjects.internal.EntityManagedActiveObjects.find(EntityManagedActiveObjects.java:   153)
  at    com.atlassian.activeobjects.osgi.DelegatingActiveObjects.find(DelegatingActiveObjects.java:81)  <+3>   (NativeMethodAccessorImpl.java:39) (DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at   org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:58)
    at     org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.j    ava:62)
 at        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:   171)
  ......
  ..
   .

当尝试以下查询时:

AData ad : ao.find(AData.class, Query.select().where("user=? AND ISSUES LIKE '%?%'  ",user,issueId)))      

还有其他方式,

     AData ad : ao.find(AData.class, Query.select().where("user=? AND ISSUES LIKE ? ",user, "%" +   
issueId + "%")))      

在上述两种情况下,出现以下错误:(主要错误是,“java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 2”)。

Uncaught exception thrown by REST service
com.atlassian.activeobjects.internal.ActiveObjectsSqlException: There was a SQL exception thrown     by the Active Objects library:
Database:
- name:HSQL Database Engine
- version:1.8.0
- minor version:8
- major version:1
Driver:
- name:HSQL Database Engine Driver
- version:1.8.0

java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 2
at     com.atlassian.activeobjects.internal.EntityManagedActiveObjects.find(EntityManagedActiveObjects.java:    153)
at     com.atlassian.activeobjects.osgi.DelegatingActiveObjects.find(DelegatingActiveObjects.java:81)  <+3>     (NativeMethodAccessorImpl.java:39) (DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.do
....

它是如何工作的......这个 LIKE 查询有什么问题?

谢谢

【问题讨论】:

    标签: jira hsqldb jira-plugin


    【解决方案1】:

    更新:LIKE 的参数是一个 SQL 字符串。您在 SQL 中对字符串使用单引号。

    您可以用参数标记替换整个参数。

    然后您为两个参数提供值。第二个参数的值应该是一个可以包含通配符元素的字符串。这个例子看起来是正确的。

    AData ad : ao.find(AData.class, Query.select().where("user=? AND ISSUES LIKE ?",user, "%" + issueId + "%")))   
    

    【讨论】:

    • 我已经更新了上面的问题以包括上面查询的结果,(请参考上面原始评论中的最后一个查询和错误)。主要是引发错误为“java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 2”。实际上,已经提供了两个参数参数和两个?在那里。虽然引发了这个错误。
    【解决方案2】:

    这是您上面代码中的类似 SQL 的语法:

    where("user=? AND ISSUES LIKE %?%",user,issueId)
    

    “%”通配符仅在用在带引号的文字或提供的参数字符串中时才有效。使用“LIKE %?%”无效。替换为:

    ISSUES LIKE ?
    

    如果此语句的参数是 'THO',您需要将其修改为 '%TH​​O%' 以获得我认为您想要的通配符函数(例如 LIKE ? 与参数集to '%TH​​O%' 将匹配 ALTHOGH、THOUGHTFUL 和 FATHOM)。

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      相关资源
      最近更新 更多