简体   繁体   中英

How to call a DB2 stored procedure passing a List<String> as param?

Java version: Java1.8
JDBC: db2jcc4-11.1.1.1
Ant1.7
Websphere8.5

I am upgrading a service to allow for passing an array of currency codes (AA, BB, CC, DD) into a stored procedure. The NSP work has been completed and verified. Here is my code below.

Mapper

<!-- Call XXXXXXX- InputArray -->
    <select id="addOrRemoveDataList" statementType="CALLABLE"
        parameterType="globalTradeAwayParams">
        {CALL ${dbPackageName}.XXXXXXX
            (#{param.ownerId, mode=IN, jdbcType=INTEGER},
            #{param.actionInd, mode=IN, jdbcType=CHAR},
            #{param.addOrRemoveElementsCount, mode=IN, jdbcType=INTEGER},
            #{param.addOrRemoveList, jdbcType=ARRAY, mode=IN, jdbcTypeName=VARCHAR, typeHandler=com.pseuplex.core.db.handlers.StringHandler})}
    </select>

StringHandler

public class StringHandler implements TypeHandler<List<String>> {
    
    // Adding logger to StringHandler
    private static Logger log = Logger.getLogger(StringHandler.class);
    
    @Override
    public void setParameter(PreparedStatement arg0, int arg1, List<String> arg2, JdbcType arg3) throws SQLException {
        // TODO Auto-generated method stub
        log.info("Prepared statement: " + arg0.toString());
        log.info("Arg1: " + arg1);
        log.info("Object: " + arg2);
        log.info("JDBC Type: " + arg3);
        
        // TODO Auto-generated method stub
        Array string = null;
        int index = 0;
        String[] stringArray;
        
        if (arg2 == null)
            stringArray = new String[0];
        else {

            stringArray = new String[arg2.size()];

            for (String i : arg2) {

                stringArray[index] = i;
                index++;
            }
        }

        Connection con = arg0.getConnection();
        
        // ERROR THROWN HERE
        string = con.createArrayOf("VARCHAR", stringArray);
        
        arg0.setArray(arg1, string);
        
    }

    @Override
    public List<String> getResult(ResultSet arg0, String arg1)
            throws SQLException {
        // TODO Auto-generated method stub
        return new ArrayList<String>();
    }

    @Override
    public List<String> getResult(ResultSet arg0, int arg1) throws SQLException {
        // TODO Auto-generated method stub
        return new ArrayList<String>();
    }

    @Override
    public List<String> getResult(CallableStatement arg0, int arg1)
            throws SQLException {
        // TODO Auto-generated method stub
        return new ArrayList<String>();
    }

}

During runtime, I am getting this error when submitting the change to my database:

FATAL com.pseuplex.j2ee.ExceptionFilter - java/sql/Connection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array; java.lang.AbstractMethodError: java/sql/Connection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array;

I have checked pages of stack overflow questions similar to this one, but none of the solutions have led me to a solution. Any help would be greatly appreciated.

Regarding the AbstractMethod error, that typically happens when you invoke an interface method which is at a higher JDBC spec level than the implementation actually implements. The java.sql.Connection.createArrayOf method was added in JDBC 4.0. Are you using an older spec level JDBC driver JAR that implements an older version such as JDBC 3.0 which would lack the createArrayOf method? That seems like a good possibility based on the error you reported. If so, just move up to a newer spec level version of the JDBC driver.

Just following up on this for future reference, I found my solution to the AbstractMethodError. I was using an old version of dbcp and ended up having to upgrade to commons-dbcp2 as well as commons-pool2.

After the upgrade, I simply changed my datasource to target dbcp2 and everything worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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