JDBC Call to a stored procedure returns null

5k Views Asked by At

I've got some problems with a stored procedure on an Oracle database.

I just want to call a procedure (which has 50 IN parameters and 2 IN OUT parameters) and get these 2 OUT parameters.

I'm trying to execute() the associated CallableStatement but it returns me a NullPointerException

java.lang.NullPointerException
at oracle.jdbc.driver.T4C8Oall.getNumRows(T4C8Oall.java:977)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1363)
...

Here is the code (I'm using Hibernate):

    Session session = (Session) HibernateUtil.currentSession();

    final Transaction transaction = session.beginTransaction();

    try {
        session.doWork(new Work() {

            public void execute(Connection connection) throws SQLException {
                try {
                    CallableStatement call = connection.prepareCall("{?,? = call geneva_admin.bksgnvcrmps.createorreturnproduct1nc(" +
                            "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                            "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                            "?,?,?,?,?,?,?,?,?,?,?,?)}");

                    int i = 1;

                    call.registerOutParameter(i++, Types.INTEGER);
                    call.registerOutParameter(i++, Types.VARCHAR);


                    call.setString(i++, typeofproduct);
                    call.setString(i++, shiptositenumber);
                    call.setString(i++, billtositenumber);
                    call.setString(i++, ordernumber);
                    call.setInt(i++, orderid);
                    ...
                    call.setInt(i++, errcode);
                    call.setString(i++, errmsg);

                    call.execute();

                    System.out.println("err_code: " + call.getString(1));
                    System.out.println("err_msg: " + call.getString(2));

                    call.close();
                    transaction.commit();
                } catch (SQLException e) {
                    throw new SQLException(e);
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        transaction.rollback();
        return false;
    }

If, before the execute(), I set

    call.setEscapeProcessing(false);

I've got a SQLSyntaxErrorException (ORA-00911: invalid character)

Could you help me ?

Fab


Here is the solution:

The 2 last parameters of the procedure are IN OUT. I thought that we had to manage them as OUT parameter :

CallableStatement call = connection.prepareCall("{?, ? = call bksgnvcrm.createorreturnproduct1nc(..., ?, ?)}");

But as they're IN OUT, these parameters have to be declared as IN :

CallableStatement call = connection.prepareCall("{call bksgnvcrm.createorreturnproduct1nc(..., ?, ?)}");

And finally register them as OUT like this :

call.registerOutParameter(i, Types.INTEGER);
call.setInt(i++, errcode);
call.registerOutParameter(i, Types.VARCHAR);
call.setString(i++, errmsg);

I hope this may help you :)

1

There are 1 best solutions below

0
On

I also had the same issue. In my case I realized that I forgot the "call" word when calling the stored procedure.

"{ call myPackage.myProcedure(?,?,?,?) }"

This may help someone else one day, who knows...