H2用户定义函数org.h2.jdbc.JdbcSQLException非十六进制字符错误

时间:2017-09-19 22:20:03

标签: java spring h2 user-defined-functions embedded-database

尝试将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]

正如日志中的代码段所示,该列表包含两个字符串:&#34; ABCD&#34;和&#34; EFGH&#34;。我试图将这些值传递给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来管理数据库连接,这里是调用方法的一小部分:

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语句的1}}子句。

关于为什么抛出这个的任何想法?

1 个答案:

答案 0 :(得分:0)

解决:

已将输入参数类型声明为Types.VARCHAR,更改为Types.JAVA_OBJECT并解决了十六进制字符问题:

StoredProcedure.declareParameter(new SqlParameter("idList", Types.JAVA_OBJECT));