Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute anonymous pl/sql block and get resultset in java

I would like to execute the anonymous PL/SQL and need to get the resultset object. I got the code which can be done by using cursors inside the PL/SQL block.

But the PL/SQL block itself will come from the database as text. So I can't edit that PL/SQL block. And it will return only two values whose column names will be same always. It will return list of 2 column combination values.

Here I am giving sample PL/SQL.

BEGIN  RETURN 'select distinct fundname d, fundname r from <table> where condition order by 1';  EXCEPTION    WHEN OTHERS THEN     RETURN 'SELECT ''Not Available'' d, ''Not Available'' r FROM dual'; END; 

Any reply will be so helpful.

like image 599
Praveen Kumar Raju Konduru Avatar asked Feb 24 '11 07:02

Praveen Kumar Raju Konduru


People also ask

When a PL SQL anonymous block is executed?

The PL/SQL anonymous block statement is an executable statement that can contain PL/SQL control statements and SQL statements. It can be used to implement procedural logic in a scripting language. In PL/SQL contexts, this statement can be compiled and executed by the data server.

How do I run an anonymous block in PL SQL Developer?

First, connect to the Oracle Database server using Oracle SQL Developer. Second, create a new SQL file named anonymous-block. sql resided in the C:\plsql directory that will store the PL/SQL code. Third, enter the PL/SQL code and execute it by clicking the Execute button or pressing the Ctrl-Enter keyboard shortcut.

How do I use anonymous block in Toad?

Open Normal SQL Editor in TOAD. Then write your anonymous pl/sql block there. Then press the DBMS output tab in the bottom portion of the screen. Press Red Dot( Turn Output on) and then select your pl/sql block and then press F9 to execute.


1 Answers

Here is a self contained example of how to "execute the anonymous PL/SQL and get the resultset object"

import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Types;  import oracle.jdbc.OracleTypes;  public class CallPLSQLBlockWithOneInputStringAndOneOutputStringParameterAndOneOutputCursorParameter {      public static void main(String[] args) throws Exception {          DriverManager.registerDriver(new oracle.jdbc.OracleDriver());          // Warning: this is a simple example program : In a long running application,         // error handlers MUST clean up connections statements and result sets.          final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "manager");         String plsql = "" +         " declare " +           "    p_id varchar2(20) := null; " +         "    l_rc sys_refcursor;" +         " begin " +         "    p_id := ?; " +         "    ? := 'input parameter was = ' || p_id;" +         "    open l_rc for " +         "        select 1 id, 'hello' name from dual " +         "        union " +         "        select 2, 'peter' from dual; " +         "    ? := l_rc;" +         " end;";          CallableStatement cs = c.prepareCall(plsql);         cs.setString(1, "12345");         cs.registerOutParameter(2, Types.VARCHAR);         cs.registerOutParameter(3, OracleTypes.CURSOR);          cs.execute();          System.out.println("Result = " + cs.getObject(2));          ResultSet cursorResultSet = (ResultSet) cs.getObject(3);         while (cursorResultSet.next ())         {             System.out.println (cursorResultSet.getInt(1) + " " + cursorResultSet.getString(2));         }          cs.close();         c.close();     } } 

The above example query "select 1 id, 'hello' name from dual union select 2, 'peter' from dual;" can be replaced by any query.

like image 181
Noctiluque Avatar answered Oct 11 '22 20:10

Noctiluque