Java reads Oracle's stored procedures

Source: Internet
Author: User
Tags getmessage sql error

Oracle's Plsql

CREATE OR REPLACE Package chapter_13 as
TYPE RS is REF CURSOR;
Procedure founder (ofields out RS);
End
CREATE OR REPLACE Package Body chapter_13 as
PROCEDURE founder (Ofields out RS) is
BEGIN
Open Ofields for
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.execute ();
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 ();
}
}

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.