jdbc - Execute anonymous pl/sql block and get resultset in java -
i execute anonymous pl/sql , need resultset object. got code can done using cursors inside pl/sql block.
but pl/sql block come database text. can't edit pl/sql block. , return 2 values column names same always. return list of 2 column combination values.
here giving sample pl/sql.
begin return 'select distinct fundname d, fundname r <table> condition order 1'; exception when others return 'select ''not available'' d, ''not available'' r dual'; end;
any reply helpful.
here self contained example of how "execute anonymous pl/sql , 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: simple example program : in long running application, // error handlers must clean connections statements , 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 = ' || p_id;" + " open l_rc " + " select 1 id, 'hello' name dual " + " union " + " select 2, 'peter' 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 dual union select 2, 'peter' dual;" can replaced query.
Comments
Post a Comment