Java reads the stored procedure of Oracle

Source: Internet
Author: User

Source: Network/Editor: getting started with programming: Unknown

Oracle PLSQL

Create or replace package chapter_13
Type RS is ref cursor;
Procedure founder (ofields out Rs );
End;
Create or replace package body chapter_13
Procedure founder (ofields out Rs) is
Begin
Open ofields
Select * From person;
End founder;
End;
Java code
Package JDBC;
Import java. Io .*;
Import java. SQL .*;
Import java. Text .*;
Import oracle. JDBC. oracletypes;
Public class teststoredprocedures {
Connection conn;
Public teststoredprocedures (){
Try {
Drivermanager. registerdriver (New Oracle. JDBC. Driver. oracledriver ());
Conn = drivermanager. getconnection (
"JDBC: oracle: thin: @ localhost: 1521: orcl", "Jola", "Jola ");
} Catch (sqlexception e ){
System. Err. println (E. getmessage ());
E. printstacktrace ();
}
}
Public static void main (string [] ARGs) throws exception {
New teststoredprocedures (). Process ();
}
Public void process () throws sqlexception {
   
Long start = 0;
Long end = 0;
Callablestatement cstmt = NULL;
Try {
Start = system. currenttimemillis ();
// *** Sql92 escape syntax ***
     
Cstmt = conn. preparecall (
"{Call chapter_13.founder (?)} ");
Cstmt. registeroutparameter (1, oracletypes. cursor );
Resultset rs = NULL;
Cstmt.exe cute ();
Rs = (resultset) cstmt. GetObject (1 );
While (Rs. Next ()){
System. Out. println (Rs. getstring ("name "));
}
Rs. Close ();
End = system. currenttimemillis ();
System. Out. println ("average elapsed time =" +
(End-Start)/8 + "milliseconds ");
} Catch (sqlexception e ){
System. Err. println ("SQL error:" + E. getmessage ());
} Finally {
If (cstmt! = NULL ){
Try {
Cstmt. Close ();
} Catch (sqlexception ignore ){}
}
}
}
Protected void finalize () throws throwable {
If (Conn! = NULL ){
Try {
Conn. Close ();
} Catch (sqlexception ignore ){}
}
Super. Finalize ();
}
}

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.