Recently, I searched many online examples of spring operations on Oracle internal objects, basically introducing the calling of the stored procedure ~ There are not many articles about directly calling Oracle internal functions ~ Here, Pharaoh draws on the example of a foreign friend on the Spring Forum and writes a demo for your reference ~
Test Oracle functions:
Here is a basic function that returns sysdate in a ref cursor:
Function my_demo_fnc
Return my_refcursor_pkg.refcursor
As
Return_date my_refcursor_pkg.refcursor;
Begin
Open return_date for 'select sysdate from dual ';
Return return_date;
End my_demo_fnc;
Test procedure:
Here is the class that accesses it: Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. util. hashmap;
Import java. util. Map;
Import javax. SQL. datasource;
Import oracle. JDBC. oracletypes;
Import org. springframework. JDBC. Core. rowcallbackhandler;
Import org. springframework. JDBC. Core. sqloutparameter;
Import org. springframework. JDBC. datasource. drivermanagerdatasource;
Import org. springframework. JDBC. Object. storedprocedure;
Public class refcursortestdao
...{
Public static void main (string [] ARGs) throws exception
...{
New refcursortestdao(cmd.exe cute ();
}
Public void execute () throws exception
...{
Datasource DS = new drivermanagerdatasource (
"Oracle. JDBC. Driver. oracledriver ",
"JDBC: oracle: thin: @ localhost: 1521: sid1 ",
"User", "password ");
Demostoredprocedure proc = new demostoredprocedure (DS );
Map Params = new hashmap ();
Proc.exe cute (Params );
}
Private class demostoredprocedure extends storedprocedure
...{
Public static final string SQL = "my_test_pkg.my_demo_fnc ";
Public demostoredprocedure (datasource DS)
...{
Setdatasource (DS );
Setsql (SQL );
Setfunction (true );
Declareparameter (
New sqloutparameter (
"Whatever", oracletypes. cursor, new demorowmapper ()));
Compile ();
}
}
Private class demorowmapper implements rowcallbackhandler
...{
Public void processrow (resultset RS) throws sqlexception
...{
System. Out. println (Rs. gettimestamp (1 ));
}
}
}
The process is the same for a real procedure with the ref cursor as an out parameter, but # setfunction shocould be false instead of true.
Reference: http://forum.springframework.org/archive/index.php/t-10054.html