从OracleJDBC调用存储过程提供异常

时间:2013-06-07 09:34:14

标签: java database oracle stored-procedures jdbc

我正在尝试使用Oracle JDBC调用Oracle的存储过程。

我是Oracle的新手,因此很难弄清楚如何调用SP并获得正确的输出。

下面的存储过程应该只给我4个没有输入的输出。

代码段是:

cStmt=connection.prepareCall(" {call userName.user(?)}");               
                    cStmt.registerOutParameter(1, OracleTypes.CURSOR);

                    cStmt.executeUpdate();                  

                    rst = (ResultSet) cStmt.getObject(1);
                    while (rst.next()) {
                        int id = rst.getInt(1);
                        int Id1=rst.getInt(2);
                        String accoutNum=rst.getString(3);
                        String accountName=rst.getString(4);
                        System.out.println("valeus");
                        System.out.println(id);
                        System.out.println(Id1);
                        System.out.println(accoutNum);
                        System.out.println(accountName);
                    }

现在运行此代码会出现以下错误:

java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'user'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
        at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:879)
        at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:505)
        at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:223)
        at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
        at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:204)
        at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1041)
        at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1328)
        at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3593)
        at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3674)
        at oracle.jdbc.driver.OracleCallableStatement.executeUpdate(OracleCallableStatement.java:4780)
        at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1354)
        at demo.Oracle.main(Oracle.java:101)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

我确信语法存在明确的问题。我google了很多,但我无法找到具体的解决方案..

有人可以指导我.. JDBC jar版本 6.0

程序标题是:

create or replace
procedure user is 
cursor user_cursor is select * from user_master;
v_rec_user user_cursor%rowtype;

1 个答案:

答案 0 :(得分:2)

您的程序定义没有参数;你调用它的方式是你希望它有一个sys_refcursor, or a schema-level cursor type类型的OUT参数。

我想你想要这样的东西:

create or replace procedure user(result out sys_refcursor) is
begin
    open result for
        select * from user_master;
end;
/

但是,由于您只显示了开始,因此不清楚您使用的是v_rec_cursor

(另外,user不是程序的好名字,因为它是一个保留字;也许类似get_users的东西会更好?)


调用该程序并打印结果的小型Java程序;这为我编译并运行(*替换为真正的数据库连接详细信息):

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.pool.OracleDataSource;

public final class DBTest
{
    public static void main(String[] args)
    throws SQLException
    {
        OracleDataSource datasource;
        Connection conn = null;
        CallableStatement cStmt = null;
        ResultSet rSet = null;

        try
        {
            datasource = new OracleDataSource();
            String jdbcURL = "jdbc:oracle:thin:*/*@*:1521:*";
            datasource.setURL(jdbcURL);
            conn = datasource.getConnection();
            cStmt = conn.prepareCall("{ call get_user(?) }");
            cStmt.registerOutParameter(1, OracleTypes.CURSOR);
            cStmt.execute();
            rSet = (ResultSet) cStmt.getObject(1);
            while (rSet.next())
            {
                System.out.println(rSet.getInt(1)
                    + ":" + rSet.getInt(2)
                    + ":" + rSet.getString(3)
                    + ":" + rSet.getString(4));
            }
        }
        finally
        {
            if (rSet != null)
            {
                rSet.close();
            }
            if (cStmt != null)
            {
                cStmt.close();
            }
            if (conn != null)
            {
                conn.close();
            }
        }
    }
}