Oracle存储过程创建及调用

本文介绍了Java如何调用Oracle数据库中的存储过程,包括无输出参数及带输出参数的情况,并展示了具体的代码示例。

在大型数据库系统中,有两个很重要作用的功能,那就是存储过程和触发器。在数据库系统中无论是存储过程还是触发器,都是通过SQL 语句和控制流程语句的集合来完成的。相对来说,数据库系统中的触发器也是一种存储过程。存储过程在数据库中运算时自动生成各种执行方式,因此,大大提高了对其运行时的执行速度。在大型数据库系统如Oracle、SQL Server中都不仅提供了用户自定义存储过程的功能,同时也提供了许多可作为工具进行调用的系统自带存储过程。
   所谓存储过程(Stored Procedure),就是一组用于完成特定数据库功能的SQL 语句集,该SQL语句集经过编译后存储在数据库系统中。在使用时候,用户通过指定已经定义的存储过程名字并给出相应的存储过程参数来调用并执行它,从而完成一个或一系列的数据库操作。
   由于J2EE体系一般建立大型的企业级应用系统,而一般都配备大型数据库系统如Oracle或者SQL Server,在本文
《JAVA与Oracle存储过程》中将介绍JAVA跟Oracle存储过程之间的相互应用跟相互间的各种调用。
   一、JAVA调用Oracle存储过程
   JAVA跟Oracle之间最常用的是JAVA调用Oracle的存储过程,以下简要说明下JAVA如何对
Oracle存储过程进行调用。
 
  Ⅰ、不带输出参数情况
      过程名称为pro1参数个数1个数据类型为整形数据
 

id="iframe_0.4290719052749483" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4290719052749483',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">  import  java.sql. * ; 
id="iframe_0.5313219447533188" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5313219447533188',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">  public    class  ProcedureNoArgs  
  { 
id="iframe_0.6598827305206152" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6598827305206152',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">  public    static    void  main(String args[])   throws  Exception 
id="iframe_0.2354899614553576" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2354899614553576',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;"> 
id="iframe_0.5519880064607328" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5519880064607328',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">    //  加载Oracle驱动  
id="iframe_0.08529654393099162" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.08529654393099162',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">   DriverManager.registerDriver(  new   oracle.jdbc.driver.OracleDriver()); 
id="iframe_0.415161237822665" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.415161237822665',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">    //  获得Oracle数据库连接  
id="iframe_0.37318599813095754" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.37318599813095754',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">   Connection conn = DriverManager.getConnection( "  jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd " ); 
id="iframe_0.8495320538065732" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8495320538065732',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.8129723794815313" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8129723794815313',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">      //  创建存储过程的对象  
id="iframe_0.08979241684974815" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.08979241684974815',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     CallableStatement c = conn.divpareCall( " {call pro1(?)} " ); 
id="iframe_0.8393817470761424" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8393817470761424',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     
id="iframe_0.2878349324429259" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2878349324429259',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">      //  给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188  
id="iframe_0.21655914258239806" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.21655914258239806',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     c.setInt( 1 , 188 ); 
id="iframe_0.9228119880280374" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9228119880280374',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     
id="iframe_0.49119695235904004" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.49119695235904004',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">      //  执行Oracle存储过程  
id="iframe_0.6600412005064376" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6600412005064376',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     c.execute(); 
id="iframe_0.9225504086918115" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9225504086918115',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">    conn.close(); 
id="iframe_0.6274990257474331" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6274990257474331',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">}  
id="iframe_0.6817984565830602" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6817984565830602',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">}  
id="iframe_0.5828580379173307" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5828580379173307',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">


   Ⅱ、带输出参数的情况
   过程名称为pro2参数个数2个数据类型为整形数据,返回值为整形类型

id="iframe_0.26930467982739303" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.26930467982739303',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;"> import java.sql.*; 
id="iframe_0.7226961627834638" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/None.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7226961627834638',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;"> public  class ProcedureWithArgs  
  { 
id="iframe_0.22799452594758152" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.22799452594758152',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;"> public  static  void main(String args[])  throws Exception 
id="iframe_0.8939903479624303" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8939903479624303',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.5665215729730066" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5665215729730066',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">    // 加载Oracle驱动 
id="iframe_0.5859873561882791" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5859873561882791',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">   DriverManager.registerDriver( new  oracle.jdbc.driver.OracleDriver()); 
id="iframe_0.8831932714478123" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8831932714478123',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">    // 获得Oracle数据库连接 
id="iframe_0.07834675769726807" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.07834675769726807',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">   Connection conn = DriverManager.getConnection(" jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd "); 
id="iframe_0.6732781504753549" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6732781504753549',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">
id="iframe_0.6069451526697318" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6069451526697318',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     // 创建Oracle存储过程的对象,调用存储过程 
id="iframe_0.1815972026553263" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1815972026553263',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">    CallableStatement c=conn.divpareCall("{call pro2(?,?)}"); 
id="iframe_0.35661093672051924" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.35661093672051924',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     
id="iframe_0.7166618658289485" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7166618658289485',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     // 给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188 
id="iframe_0.9772757742868532" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9772757742868532',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">    c.setInt(1,188); 
id="iframe_0.43708143715982284" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.43708143715982284',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     //注册存储过程的第二个参数  
      c.registerOutParameter(2,java.sql.Types.INTEGER);
id="iframe_0.6864837140123028" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6864837140123028',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">     // 执行Oracle存储过程 
id="iframe_0.14373047718205534" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.14373047718205534',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">    c.execute(); 
      // 得到存储过程的输出参数值并打印出来
      System.out.println (c.getInt(2));
 
id="iframe_0.53167971120836" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.53167971120836',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">    conn.close(); 
id="iframe_0.8519141161166681" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8519141161166681',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">} 
id="iframe_0.41087735794683056" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.41087735794683056',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">} 

 

 

Oracle存储过程包含三部分:过程声明,执行过程部分,存储过程异常。

Oracle存储过程可以有无参数存储过程和带参数存储过程。 
、无参程序过程语法

1  create  or  replace  procedure NoParPro
2  as   id="iframe_0.4691381334482725" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/dot.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4691381334482725',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 15px; height: 20px;">;
3  begin
4  id="iframe_0.27729710386688877" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/dot.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.27729710386688877',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">;
5 exception     //存储过程异常
6      id="iframe_0.8391422049936075" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://www.blogjava.net/Images/dot.gif?_=1776094%22%20style=%22border:none;max-width:1011px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8391422049936075',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">;
7  end;
8 

        二、带参存储过程实例

 1  create  or  replace  procedure queryempname(sfindno emp.empno %type)  as
 2        sName emp.ename %type;
 3        sjob emp.job %type;
 4  begin
 5        ....
 7 exception
          ....
14  end;
15 

    三、 带参数存储过程含赋值方式
 1  create  or  replace  procedure runbyparmeters  (isal  in emp.sal %type, 
                            sname out  varchar,sjob  in out  varchar)
 2   as icount  number;
 3   begin
 4        select  count( *into icount  from emp  where sal >isal  and job =sjob;
 5        if icount = 1  then
 6         ....
 9        else
10         ....
12        end  if;
13  exception
14        when too_many_rows  then
15       DBMS_OUTPUT.PUT_LINE( ' 返回值多于1行 ');
16        when others  then
17       DBMS_OUTPUT.PUT_LINE( ' 在RUNBYPARMETERS过程中出错! ');
18   end;
19 

  四、在Oracle中对存储过程的调用
  过程调用方式一
 1  declare
 2        realsal emp.sal %type;
 3        realname  varchar( 40);
 4        realjob  varchar( 40);
 5   begin   //存储过程调用开始
 6        realsal: = 1100;
 7        realname: = '';
 8        realjob: = ' CLERK ';
 9        runbyparmeters(realsal,realname,realjob);     --必须按顺序
10        DBMS_OUTPUT.PUT_LINE(REALNAME || '     ' ||REALJOB);
11   END;  //过程调用结束
12 

  过程调用方式二
 1  declare
 2       realsal emp.sal %type;
 3       realname  varchar( 40);
 4       realjob  varchar( 40);
 5  begin    //过程调用开始
 6       realsal: = 1100;
 7       realname: = '';
 8       realjob: = ' CLERK ';
 9       runbyparmeters(sname =>realname,isal =>realsal,sjob =>realjob);  --指定值对应变量顺序可变
10       DBMS_OUTPUT.PUT_LINE(REALNAME || '     ' ||REALJOB);
11  END;  //过程调用结束
12 

 

 至此,有关ORACLE的基本存储过程以及对Oracle存储过程的调用方式介绍完毕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值