Solved

How to get Return String from Oracle Procedure  in Java class

Posted on 2004-04-02
3
2,104 Views
Last Modified: 2007-12-19
Hi

Iam Calling Stored Procedure  (Oracle 9i, J2ee)
Object of Stored procedure to insert Data into a table in this process it makes 2 calls to other tables get relevant data
finally insert Row into target table.

In my procedure i have appended Error messages with separated by ","

How can i get this return string

Here is my code
strSql = "{? = call package.proc_name(?,?,?,?,?,?,?,?)}";
cstmt = connection.prepareCall(strSql);
cstmt.setLong(1, Long.parseLong(form.searchYear));
..............................

cstmt.executeUpdate();

How to get return string from Procedure

-Lakshmana
0
Comment
Question by:LakshmanaRavula
3 Comments
 
LVL 5

Assisted Solution

by:mudumbe
mudumbe earned 75 total points
ID: 10744320
You should be able to use:

cstmt.registerOutParameter(1,Types.CHAR);

cstmt.executeUpdate();

String result = cstmt.getString(1);
0
 
LVL 8

Accepted Solution

by:
william_jwd earned 75 total points
ID: 10746830
try this,

OracleCallableStatement ocs = (OracleCallableStatement) conn.prepareCall( "{ ? = call Proc_Name(?)}");
        ocs.setString(2,"DTD_REFERENCE");
        ocs.registerOutParameter(1,OracleTypes.LONGVARCHAR);
        ocs.execute();
        String str = (String) ocs.getString(1);
0
 
LVL 1

Assisted Solution

by:Dazman
Dazman earned 100 total points
ID: 10751891
Heres a runnable example, my Java class will only set up the callable statement if its not already done (saves parse time!):

SQL> REM create the function that the java will call
SQL> create or replace function j_test(a in varchar2)
  2    return varchar2
  3  is
  4  begin
  5    return 'hello from Oracle';
  6  end;
  7  /

Function created.

SQL> REM now create the java.
SQL> CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "JavaTest" AS
  2  // Packages for JDBC related Classes
  3  import oracle.jdbc.driver.*;
  4  import oracle.sql.*;
  5  import java.sql.*;
  6  import java.sql.Connection;
  7  import java.sql.SQLException;
  8  
  9  public class JavaTest
 10  {
 11    /* Static variables */
 12    private static Connection conn;        /** Connection to the remote server. */
 13    private static CallableStatement stmt; /** Statement to call Geneva with. */
 14  
 15    private static String host = "147.149.178.73";
 16    private static String portNum = "1521";
 17    private static String sid = "spgd1";
 18    private static String uname = "lindled";
 19    private static String pword = "duaij6";
 20  
 21    public static void connect()
 22    throws SQLException
 23    {
 24      // Establish a connection if we do not have one.
 25      if (conn == null)
 26      {
 27        // Register the Oracle JDBC driver.
 28        DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
 29        // Establish a connection to the database.
 30        conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":"
 31                                           + portNum + ":" + sid,
 32                                          uname, pword);
 33      }
 34    }
 35    
 36    
 37    /**
 38     * This method prepares the anonymous block to send to Geneva.
 39     */
 40    private static void prepareCall()
 41    throws SQLException
 42    {
 43      // If the statment has not been initialised then initialise it.
 44      if (stmt == null)
 45      {
 46        stmt = conn.prepareCall("begin ? := j_test(?); end;");
 47        stmt.registerOutParameter(1, OracleTypes.LONGVARCHAR);
 48      }
 49    }
 50    
 51    
 52    public static void sendRequest()
 53    throws SQLException
 54    {
 55      // Connect.
 56      connect();
 57      
 58      // Create a callable statement.
 59      prepareCall();
 60      
 61      // Register the input.
 62      stmt.setString (2, "hello world");
 63      
 64      try
 65      {
 66        // Run the call.
 67        stmt.execute();
 68      }
 69      catch(SQLException e)
 70      {
 71        conn = null;
 72        stmt = null;
 73        throw e;
 74      }
 75      
 76      // Obtain the output value.
 77      System.out.println(stmt.getString(1));
 78      
 79    }
 80  }
 81  /

Java created.

SQL> REM create a wrapper for the java so i can test it from SQLplus
SQL> create or replace procedure call_java
  2  is language java
  3  name 'JavaTest.sendRequest()';
  4  /

Procedure created.

SQL> REM this will fail as the java state is changed
SQL> exec call_java;
BEGIN call_java; END;

*
ERROR at line 1:
ORA-29549: class LINDLED.JavaTest has changed, Java session state cleared
ORA-06512: at "LINDLED.CALL_JAVA", line 0
ORA-06512: at line 1


SQL> REM enable output
SQL> set serverout on
SQL> exec dbms_java.set_output(10000);

PL/SQL procedure successfully completed.

SQL>
SQL> REM call the java class we should see "hello from Oracle" come back
SQL> exec call_java;
hello from Oracle

PL/SQL procedure successfully completed.

SQL>
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle sql query 7 61
Use of Exception to end a Loop 3 31
Import and exporting Oracle Data with encrypted columns 4 29
SQL2016 to ORACLE11G linked-server 6 15
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question