我需要执行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查询有什么问题?
谢谢
答案 0 :(得分:1)
更新:LIKE的参数是一个SQL字符串。您在SQL中使用单引号作为字符串。
您可以使用参数标记替换整个参数。
然后为两个参数提供值。第二个参数的值应该是一个字符串,其中可以包含通配符元素。这个例子看起来是正确的。
AData ad : ao.find(AData.class, Query.select().where("user=? AND ISSUES LIKE ?",user, "%" + issueId + "%")))
答案 1 :(得分:0)
这是您在上面的代码中使用的类似SQL的语法:
where("user=? AND ISSUES LIKE %?%",user,issueId)
'%'通配符仅在引用的文字或提供的参数字符串中使用时有效。用法“LIKE%?%”无效。替换为:
ISSUES LIKE ?
如果此语句的参数为'THO',则需要将其修改为'%THO%'才能获得我认为您想要的通配符函数(例如,LIKE?with the parameter set '%THO%'会匹配,尽管,但仍然和FATHOM相同。