使用Oracle SQL Developer调用存储过程

时间:2015-06-01 13:13:56

标签: java sql oracle jdbc

在运行Java代码时,我无法将服务器输出设置为ON。但我能够连接到Oracle SQL数据库。

获得以下错误:

  

**** java.lang.NullPointerException DB已成功连接java.sql.SQLSyntaxErrorException:ORA-00922:缺少或无效   选项****

请建议我一个解决方案。

代码:

ResultSet rs= DS_DB.getResultSet("set serveroutput on");
DS_DB.connectDatabase();

String command = "{call sfmd.getNativeIdsForXID('EDDAC')}";
CallableStatement cstmt = connection.prepareCall(command);
cstmt.execute();
System.out.println("Cstmt :"+command);'

由于

2 个答案:

答案 0 :(得分:0)

有点不清楚你在这里尝试做什么。

如果您查找结果集,Oracle不返回结果集,则需要将其中一个参数定义为SYS_REFCURSOR类型的输出参数。

String command = "{call sfmd.getNativeIdsForXID(?,?)}";
CallableStatement cstmt = connection.prepareCall(command);
cstmt.registerOutParameter(1, ORACLE_CURSOR_TYPE);
cstmt.setObject(2, 'EDDAC');
cstmt.execute();

ResultSet rs = (ResultSet) cstmt.getObject(1);

在您的存储过程中,定义输出参数

CREATE OR REPLACE PROCEDURE "getNativeIdsForXID" (
    NativeIdsCursor OUT SYS_REFCURSOR, 
    P_XID VARCHAR(20)) IS

  open NativeIdsCursor for 
  SELECT ID from SomeTable where XID = P_XID;

END "getNativeIdsForXID";

如果在存储过程中使用dbms_output包,也可以获得输出。

这里有几个实用程序可以做到这一点。您启用dbms输出,然后像往常一样执行您的过程,然后您可以打印输出。

    public static void enableDBMSOutput(DBConnection conn, int buffer_size) {

        try {

            String SQLString = null;

            if (buffer_size > 0)
                SQLString = "sys.dbms_output.enable(?)";
            else
                //SQLString = "{call sys.dbms_output.enable(buffer_size => NULL) }"; // Unlimited
                SQLString = "sys.dbms_output.enable(buffer_size => NULL)"; // Unlimited

            CallableStatement stmt = conn.getCallableStatement(true, SQLString);

            if (buffer_size > 0)
                stmt.setInt(1,buffer_size);

            stmt.execute();

            System.out.println("DBMS_OUTPUT has been Enabled!");

        } catch (SQLException e) {
            System.out.println("Problem occurred enabling dbms_output: " + e.toString());
        }
    }


    public static void disableDBMSOutput(DBConnection conn) {

        try {

            String SQLString = "sys.dbms_output.disable()";

            CallableStatement stmt = conn.getCallableStatement(true, SQLString);

            stmt.execute();

            System.out.println("DBMS_OUTPUT has been Disabled!");

        } catch (SQLException e) {
            System.out.println("Problem occurred disabling dbms_output: " + e.toString());
        }
    }


    public static void printDBMSOutput(DBConnection conn) {

        try {
            String SQLString = "sys.dbms_output.get_line(?,?)";

            CallableStatement stmt = conn.getCallableStatement(true, SQLString);

            stmt.registerOutParameter(1,java.sql.Types.VARCHAR);
            stmt.registerOutParameter(2,java.sql.Types.NUMERIC);

            int status = 0;
            String output = null;

            do {
                stmt.execute();
                output = stmt.getString(1);
                status = stmt.getInt(2);

                if (null != output)
                    System.out.println("dbms_output: " + output);

            } while (status == 0);

        } catch (Exception e) {
            System.out.println("Problem occurred during print_dbms_output: " + e.toString());
        }
    }

    public static String [] getDBMSOutput(DBConnection conn) {

        try {
            String SQLString = "{call sys.dbms_output.get_line(?,?)}";

            CallableStatement stmt = conn.getCallableStatement(true, SQLString);

            stmt.registerOutParameter(1,java.sql.Types.VARCHAR);
            stmt.registerOutParameter(2,java.sql.Types.NUMERIC);

            int status = 0;
            String output = null;
            ArrayList <String> dbmsOutput = new ArrayList<String>();
            do {
                stmt.execute();
                output = stmt.getString(1);
                status = stmt.getInt(2);

                if (null != output)
                    dbmsOutput.add(output);

            } while (status == 0);

            return (String [])dbmsOutput.toArray();

        } catch (Exception e) {
            System.out.println("Problem occurred during print_dbms_output: " + e.toString());
            return null;
        }
    }
}

public CallableStatement getCallableStatement(boolean isProcedure, String sqlString){
    try {
        if (isProcedure) {
            String query = "{ call " + sqlString + " }";

            return dbConn.prepareCall(query);
        } else
            return dbConn.prepareCall(sqlString);

    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}

答案 1 :(得分:0)

set serveroutput on是一个SQL * plus命令,在JDBC驱动程序中不可用。 JDBC驱动程序不支持dbms_output。 Tom Kyte的网站有一个很好的解释和可能的解决方案。 https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:45027262935845