select子句中参数的未知数据类型错误

时间:2016-10-16 17:44:57

标签: java jdbc prepared-statement

我想使用以下代码查询我的数据库(H2):

PreparedStatement x = connection.prepareStatement("INSERT INTO tree(ancestor, desc) (select ancestor,? from tree where desc = ? union all select ?,? )");
x.setInt(1,99219);
x.setInt(2,4);
x.setInt(3,99219);
x.setInt(4,99219);

但它不起作用,我收到以下错误:

org.h2.jdbc.JdbcSQLException: Unknown data type: "?, ?"; SQL statement:insert into tree(ancestor, desc) (select ancestor,? from tree where desc = ? union all select ?,? ) [50004-192]

在控制台中执行查询(不使用JDBC)可以正常工作。这段代码有什么问题?

2 个答案:

答案 0 :(得分:0)

"INSERT INTO tree(ancestor, desc) (select ancestor,? from tree where desc = ? union all select ?,? )"

此SQL语句不完整。在此之后,它正在调整FROM部分:

union all select ?,?

答案 1 :(得分:0)

UNION select返回的列必须按类型匹配第一个选择中的列。你可以做一个明确的演员。