Domanda

I'm trying to execute a query against z/OS DB2 using JDBC Type 4 Connection. 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.

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?

È stato utile?

Soluzione

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. 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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top