尝试将List<String>
传递给H2用户定义的函数时,我会收到以下错误:
[{call MY_USER_DEFINED_FUNCTION(?)}]; SQL state
[90004]; error code [90004]; Hexadecimal string contains non-hex character:
"[ABCD, EFGH]"; SQL statement:
call MY_USER_DEFINED_FUNCTION(?) [90004-192]; nested
exception is org.h2.jdbc.JdbcSQLException: Hexadecimal string contains non-
hex character: "[1234, 5678]"; SQL statement:
call MY_USER_DEFINED_FUNCTION(?) [90004-192]
如日志显示的摘要时,列表包含两个字符串:" abcd"one_answers" efgh"。我正在尝试将这些值传递到SQL语句的IN
子句中。用户定义的功能如下:
CREATE ALIAS MY_USER_DEFINED_FUNCTION AS
'
java.sql.ResultSet getTableContent(java.sql.Connection con, List<String> idList) throws Exception {
final PreparedStatement statement = con.prepareStatement("SELECT * FROM MY_TABLE WHERE id IN (?)");
String ids[] = new String[idList.size()];
idList.toArray(ids);
statement.setArray(1, con.createArrayOf("text", ids));
java.sql.ResultSet rs = statement.executeQuery();
return rs;
}'
从DAO方法调用用户定义的功能。我正在使用Spring来管理DB连接,这是调用方法的片段:
public List<MyObject> getMyObjects(final List<String> idList) {
Map<String, Object> params = new HashMap<>();
params.put("idList", idList);
// Execute stored procedure
Map<String, Object> output = super.execute(params);
// Collect stored procedure results into list
List<MyObject> myObjectList = (List<MyObject>) output.get("myResultSet");
return myObjectList;
}
我还尝试传递一个数组,并在准备好的语句中使用setObject
- 抛出了相同的错误。我能够使用户定义的功能在传递单字符串时返回,并在准备好的语句中调用setString
,但似乎无法将多个字符串传递给SQL语句的IN
条款。
关于为什么要抛出的任何想法?
已解决:
已将输入参数类型称为Types.VARCHAR
,更改为Types.JAVA_OBJECT
并解决了十六进制字符问题:
StoredProcedure.declareParameter(new SqlParameter("idList", Types.JAVA_OBJECT));