DB2 query error during the retrieval of a CLOB fie

2019-03-01 09:57发布

问题:

From Java I am doing the following query on DB2:

SELECT * FROM PRV_PRE_ACTIVATION WHERE TRANSACTION_ID = ?

The field TRANSACTION_ID is a VARCHAR of length 32. I set the parameter in the preparedStatement using the setString method.

I get the error:

com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-270, SQLSTATE=42997, SQLERRMC=63, DRIVER=3.59.81
    at com.ibm.db2.jcc.am.dd.a(dd.java:676)
    at com.ibm.db2.jcc.am.dd.a(dd.java:60)
    at com.ibm.db2.jcc.am.dd.a(dd.java:127)
    at com.ibm.db2.jcc.am.bn.c(bn.java:2546)
    at com.ibm.db2.jcc.am.bn.d(bn.java:2534)
    at com.ibm.db2.jcc.am.bn.a(bn.java:2026)
    at com.ibm.db2.jcc.t4.cb.g(cb.java:140)
    at com.ibm.db2.jcc.t4.cb.a(cb.java:40)
    at com.ibm.db2.jcc.t4.q.a(q.java:32)
    at com.ibm.db2.jcc.t4.rb.i(rb.java:135)
    at com.ibm.db2.jcc.am.bn.gb(bn.java:1997)
    at com.ibm.db2.jcc.am.cn.pc(cn.java:3009)
    at com.ibm.db2.jcc.am.cn.b(cn.java:3786)
    at com.ibm.db2.jcc.am.cn.bc(cn.java:678)
    at com.ibm.db2.jcc.am.cn.executeQuery(cn.java:652)

Where the sqstate means "Capability is not supported by this version of the DB2 application requester, DB2 application server, or the combination of the two." But I don't use any strange functionality.

I have tried using an squ client the query:

SELECT * FROM PRV_PRE_ACTIVATION where transaction_id='A'

And it goes ok.

What is the cause of the problem?

UPDATE: The code where the statement is prepared:

s = con.prepareStatement(sSQL,
                 ResultSet.TYPE_SCROLL_INSENSITIVE,
                 ResultSet.CONCUR_UPDATABLE);

回答1:

Try changing to a specified list of columns in the select list -- my guess is you have a user defined column type (or some other type) which is not supported by your driver. For example, does the statement

SELECT TRANSACTION_ID FROM PRV_PRE_ACTIVATION WHERE TRANSACTION_ID = ?

work? If so then start adding columns in and you will find the problem column.



回答2:

I've came across this problem lately, and after some searching on web, I've came across this link: DB2 SQL error: SQLCODE: -270, SQLSTATE: 42997, SQLERRMC: 63 , which specifies this:

A column with a LOB type, distinct type on a LOB type, or structured type cannot be specified in the select-list of an insensitive scrollable cursor.

With help from an colleague, we came to this conclusion:

1, Q: When will you get this "SQLCODE=-204, SQLSTATE=42704" exception?

A: When a scrollable PreparedStatement is prepared & executed, yet there are [B|C]LOB fields exist in the select list. e.g.:

String strQuery = "SELECT NUMBER_FIELD, CHAR_FIELD, CLOB_FIELD FROM TABLE_NAME WHERE CONDITION IS TRUE;"
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, REsultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(strQuery); //and this exception will be thrown here

2, Q: So what's the solution if we want to get rid of it when [B|C]LOB fields are queried?

A: Try to use ResultSet.TYPE_FORWARD_ONLY while creating the query statement.e.g.:

stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

Or simply try this one:

stmt = conn.createStatement();

Note that the same rules apply to conn.prepareStatement() too. You may refer to Java API doc for more information.