简体   繁体   中英

Calling a Stored Procedure Using Oracle SQL Developer

I am not able to set the server output to ON , while running the Java code. But I am able to connect to Oracle SQL database.

Getting below error:

****java.lang.NullPointerException DB Connected successfully java.sql.SQLSyntaxErrorException: ORA-00922: missing or invalid option****

Please suggest me a solution.

Code:

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

Thanks

It's a little unclear what your trying to do here.

If your looking for the result set, Oracle doesn't return result sets, you need to define one of your parameters as an output parameter of type 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);

In your Stored Procedure, define the output parameter

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";

You can also get output if you use the dbms_output package in your stored procedure.

Here's a couple of utilities to do that. You enable the dbms output, then execute your procedure like you normally would, then you can print the output afterward.

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

set serveroutput on is a SQL*plus command and not available in JDBC driver. JDBC driver is not dbms_output aware. Tom Kyte's site has a nice explanation and possible solution. https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:45027262935845

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM