2

我需要执行 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:JDBC 调用中的参数无效:参数索引超出范围: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 查询有什么问题?

谢谢你

4

2 回答 2

1

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

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

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

AData ad : ao.find(AData.class, Query.select().where("user=? AND ISSUES LIKE ?",user, "%" + issueId + "%")))   
于 2013-05-24T15:28:46.563 回答
0

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

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

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

ISSUES LIKE ?

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

于 2013-05-30T05:45:53.567 回答