存储过程和准备好的语句错误

时间:2012-09-27 02:34:49

标签: java stored-procedures db2 prepared-statement

我从Java应用程序调用存储过程,并在设置输入参数时收到以下错误。

1:调用过程的Java方法:

public int callProc(String userid, String program_id, String procName)
        throws SQLException {

    int returnCode = -1;

    try {

        CallableStatement stmt;

        String sql = "CALL " + procName + "(?,?,?,?,?)";
        stmt = connection.prepareCall(sql);

        System.out.println("Calling stored procedure [" + procName + "]");

        // call setXXX() method to set values for input parameters
        // and input-output parameters
        System.out.println("jdbcadapter->callproc setting 1 parameter "+stmt);
        stmt.setObject(1, userid, java.sql.Types.CHAR);
        System.out.println("jdbcadapter->callproc setting 2 parameter "+sql);
        stmt.setObject(2, program_id, java.sql.Types.CHAR);
        System.out.println("jdbcadapter->callproc setting 3 parameter "+sql);
        stmt.setObject(3, session_id, java.sql.Types.CHAR);
        System.out.println("jdbcadapter->callproc setting 4 parameter "+sql);
        stmt.setObject(4, " ", java.sql.Types.CHAR);
        System.out.println("jdbcadapter->callproc setting 5 parameter "+sql);
        stmt.setObject(5, " ", java.sql.Types.CHAR);
        System.out.println("jdbcadapter->callproc setting 6 parameter "+sql);
        // call registerOutParameter() method to define the data types for output parameters
        stmt.registerOutParameter(4, java.sql.Types.CHAR); // rc
        System.out.println("jdbcadapter->callproc setting 7 parameter "+sql);
        stmt.registerOutParameter(5, java.sql.Types.CHAR); // result message
        System.out.println("jdbcadapter->callproc setting 8 parameter "+sql);

        stmt.execute();
        System.out.println("jdbcadapter->callproc after execute "+sql);

        String rc = stmt.getString(4);
        System.out.println("jdbcadapter->callproc after execute "+rc);
        String message = stmt.getString(5);
        System.out.println("jdbcadapter->callproc after execute "+message);
        Integer return_code = new Integer(rc.trim());
        System.out.println("jdbcadapter->callproc after execute "+return_code);
        returnCode = return_code.intValue();
        System.out.println("jdbcadapter->callproc after execute "+returnCode);
        return_message = message.trim();
        System.out.println("jdbcadapter->callproc after execute "+return_message);
        System.out.println("Returned code from stored procedure is ["+ rc.trim() + "]");
        stmt.close();

    } catch (Exception e) {
        throw ((SQLException) e);
    }

    return returnCode;

} // end callProc()

在stmt at

中设置USER ID时出现问题
stmt.setObject(1, userid, java.sql.Types.CHAR);

数据库端存储过程的定义

CREATE PROCEDURE "CDR     "."SPUBASE"
                (
                IN      USERID   CHARACTER(12),
                IN      PROGRAM_ID   CHARACTER(20),
                IN      SESSION_ID   CHARACTER(27),
                OUT     SQLCODE   CHARACTER(254),
                OUT     SQLMESSAGE   CHARACTER(254)
                )
                DYNAMIC RESULT SETS 0
                SPECIFIC SQL040816123726847
                EXTERNAL NAME '/cdrtst/sqllib/function/SPUBASE!SPUBASE'
                LANGUAGE C
                PARAMETER STYLE DB2DARI
                NOT DETERMINISTIC
                FENCED NOT THREADSAFE
                MODIFIES SQL DATA
                NO DBINFO;

,错误是

SQLException: DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=SPUBASE;PROCEDURE, DRIVER=4.1.85 

任何人都可以提出可能导致这种情况的原因。

1 个答案:

答案 0 :(得分:1)

page解释SQLCODE: -440的原因,其中一个原因是

  

参数的正确数量包含在例程引用中,但是一个或多个参数的数据类型不正确。

我怀疑您的以下方法调用

stmt.setObject(1, userid, java.sql.Types.CHAR);

应替换为

stmt.setString(1, userid);

stmt.setObject(1, userid, java.sql.Types.VARCHAR);

我更喜欢前者,即setString,并将其留给驱动程序,以根据底层数据库/列进行适当的转换。

同样适用于其他statement.set*方法,调用适当的set *方法,如setString,setInt等。