0

I am trying to invoke stored procedure of Oracle using Oracle JDBC.

I am very new to Oracle and hence having difficulty figuring out how to invoke the SP and get the proper output.

The Stored procedure below is supposed to give me just 4 outputs with no input.

Code snippet is :

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);
                    }

Now running this code is giving me following error:

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)

I am sure there's a definite problem with the syntax. I googled a lot but I am not able to find the concrete solution..

Can someone please guide me.. JDBC jar version is 6.0

the Header of procedure is :

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

1 回答 1

2

您的过程定义没有参数;你调用它的方式是你期望它有一个类型的 OUT 参数sys_refcursor,或者一个模式级游标类型

我想你想要这样的东西:

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();
            }
        }
    }
}
于 2013-06-07T10:42:18.810 回答