Spring calls Oracle's Function

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.