Java connection Oracle execution stored procedure applet

Source: Internet
Author: User

The following is a small routine where the Java connection Oracle executes a stored procedure that does not return a value.

package com.test;import java.sql.*;p ublic class procedure {     Public static void main (String[] args)  {         Connection ct=null;        CallableStatement cs=null;         try {             class.forname ("Oracle.jdbc.driver.OracleDriver");                            ct=drivermanager.getconnection ("JDBC:ORACLE:THIN:@10.8.2.73:1521:ORCL",              "Scott", "123456");             cs=ct.preparecall ("{Call pro_book (?,?,?)}");              cs.setint (1, 10001);             Cs.setstring (2,  "The Wolf of Wall Street");             Cs.setstring (3,  "Citic Press");             Cs.execute ();          }                     catch  ( Classnotfoundexception e)  {                      e.printstacktrace ();         }         catch  (sqlexception e)  {              e.printstacktrace ();         }        finally{                          try {                 cs.close ();                 ct.close ();             } catch  (Sqlexception e)  {                                   E.printstacktrace ();            }         }              } }

The following is a small program in Java that executes a stored procedure with a return value.

Package Com.test;import java.sql.*;p Ublic class procedure {public static void main (string[] args) {Connection ct=null; CallableStatement cs=null;try {class.forname ("Oracle.jdbc.driver.OracleDriver"); Ct=drivermanager.getconnection (" Jdbc:oracle:thin:@10.8.2.73:1521:orcl "," Scott "," 123456 "), Cs=ct.preparecall (" {Call Pro_book_getname (?,?)} "); Cs.setint (1, 10001); Cs.registeroutparameter (2, Oracle.jdbc.OracleTypes.VARCHAR); Cs.execute (); String name=cs.getstring (2);   SYSTEM.OUT.PRINTLN (name);} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();} Finally{try {cs.close (); Ct.close ();} catch (SQLException e) {e.printstacktrace ();}}}}

A Java program that gets a collection:

First write an Oracle package, define a cursor type, PL/SQL as follows:

Create Package pack_cursor are type type_cursor is ref cursor;end; /

Write a stored procedure again:

Create or Replace procedure Pro_emp_cursor (No in number,cur off pack_cursor.type_cursor) isbegin Open cur for select * FR  Om emp where deptno=no;  End /

The Java program code is as follows:

package com.test;import java.sql.*;p ublic class  Procedure {public static void main (String[] args)  {Connection ct=null; Callablestatement cs=null;try {class.forname ("Oracle.jdbc.driver.OracleDriver"); ct= Drivermanager.getconnection ("Jdbc:oracle:thin:@10.8.2.73:1521:orcl", "Scott", "123456"); Cs=ct.preparecall ("{Call  pro_emp_cursor (?,?)} "); Cs.setint (1, 10); Cs.registeroutparameter (2, oracle.jdbc.oracletypes.cursor); Cs.execute (); Resultset rs= (ResultSet) Cs.getobject (2), while (Rs.next ()) {System.out.println ("EMPNO:" +rs.getint (1) + "  NAME: "+rs.getstring (2) +"  job: "+rs.getstring (3));}}    catch  (classnotfoundexception e)  {e.printstacktrace ();}  catch  (sqlexception e)  {e.printstacktrace ();} Finally{try {cs.close (); Ct.close ();}  catch  (sqlexception e)  {e.printstacktrace ();}}} 


This article is from the "Flying Fish Technology" blog, please be sure to keep this source http://flyingfish.blog.51cto.com/9580339/1584464

Java connection Oracle execution stored procedure applet

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.