简体   繁体   English

DB2 V10 zOS JDBC获得SQLCODE -302

[英]DB2 V10 zOS JDBC getting SQLCODE -302

I'm trying to execute a query against z/OS DB2 using JDBC Type 4 Connection. 我正在尝试使用JDBC Type 4 Connection对z / OS DB2执行查询。 The query is simplified: 该查询已简化:

PreparedStatement stmt = conn.prepareStatement("SELECT * FROM EMP WHERE ? = ' ' OR NAME = ?");  
    stmt.setString(1, "Joe");  
    stmt.setString(2, "Joe");

Running this Query will result in an SQLCODE -302 because Joe is length three and one blank is length one. 运行此查询将导致SQLCODE -302,因为Joe的长度为3,一个空格的长度为1。

If I chnage the Query to 如果我将查询更改为

"SELECT * FROM EMP WHERE ? = '   ' OR NAME = ?"
it runs without error but that is not what I'm looking for. 它运行没有错误,但这不是我想要的。
Is there maybe a JDBC property which makes the expression ?=' ' universal for any length of the parameter? 也许有一个JDBC属性可以使表达式?=' '对于任何长度的参数都通用吗?

Since there's no information in the bare parameter marker as to the exact data type or precision of the parameter, the DB2 server uses the right side of the expression to determine it. 由于裸参数标记中没有关于参数的确切数据类型或精度的信息,因此DB2服务器使用表达式的右侧来确定它。 You could use an explicit cast to provide the necessary information to the query compiler: 您可以使用显式转换来向查询编译器提供必要的信息:

SELECT * FROM EMP WHERE cast(? as varchar(10)) = ' ' OR NAME = ?"

Replace varchar(10) with the actual data type of EMP.NAME . varchar(10)替换为EMP.NAME的实际数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM