使用选择和参数执行 DB2 插入
我想做这样的事情:
INSERT INTO TABLEA
(
COLUMN1, COLUMN2, COLUMN 3
)
SELECT FOOBAR, DOOBAR, ?
FROM TABLEB
然后通过 Spring JDBC 将其发送到 JDBC 进行更新...
simpleJdbcTemplate.update( mySqlFromAbove, someVariableToReplaceQuestionMark );
这可能吗?如果我在构建 SQL 查询时将问号替换为硬编码值,效果会很好,但我不想让自己接受 SQL 注入...
编辑 -
我明白
嵌套异常为 com.ibm.db2.jcc.c.SqlException:DB2 SQL 错误:SQLCODE:-418,SQLSTATE:42610,SQLERRMC:null
这似乎表明
参数标记的使用无效?
I want to do something like this:
INSERT INTO TABLEA
(
COLUMN1, COLUMN2, COLUMN 3
)
SELECT FOOBAR, DOOBAR, ?
FROM TABLEB
And then send this to JDBC via Spring JDBC to update...
simpleJdbcTemplate.update( mySqlFromAbove, someVariableToReplaceQuestionMark );
Is this even possible? It would work fine if I replace the question mark with the hardcoded value when building my SQL query, but I don't want to open myself to SQL injection...
Edit -
I get
nested exception is com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -418, SQLSTATE: 42610, SQLERRMC: null
Which seems to indicate
Invalid use of a parameter marker ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要对参数标记进行类型转换,以便 DB2 知道会发生什么。
例如:
显然,转换为适当的类型——int只是一个例子。
You need to type-cast your parameter marker so DB2 knows what to expect.
For example:
Obviously, cast to the appropriate type -- int is just an example.
这是 DB2 SQL 消息参考。以下是您检索到的 SQLCODE 和 SQLSTATE 的相关摘录:
不幸的是,这并不能回答你的问题,因为你的 SQL 看起来不错。在谷歌搜索之后,它看起来更像是这样DB2 JDBC 驱动程序根本不使用
PreparedStatement
中的INSERT INTO ... SELECT ...
语句。目前尚不清楚 SQL 消息参考中是否缺少该内容,或者 JDBC 驱动程序中是否存在错误。Here's the DB2 SQL Message Reference. Here's an extract of relevance for the SQLCODE and SQLSTATE you retrieved:
Unfortunately this doesn't answer your problem since your SQL seem to look fine. After Googling a bit more it look more like that the DB2 JDBC driver simply doesn't eat
INSERT INTO ... SELECT ...
statements in aPreparedStatement
. It's unclear if that is missing in the SQL Message Reference or a bug in the JDBC driver.