在大型数据库系统中,有两个很重要作用的功能,那就是存储过程和触发器。在数据库系统中无论是存储过程还是触发器,都是通过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.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.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存储过程可以有无参数存储过程和带参数存储过程。
一、无参程序过程语法
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
二、带参存储过程实例
2 sName emp.ename %type;
3 sjob emp.job %type;
4 begin
5 ....
7 exception
....
14 end;
15
三、 带参数存储过程含赋值方式
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中对存储过程的调用
过程调用方式一
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
过程调用方式二
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存储过程的调用方式介绍完毕。
本文介绍了Java如何调用Oracle数据库中的存储过程,包括无输出参数及带输出参数的情况,并展示了具体的代码示例。
1943

被折叠的 条评论
为什么被折叠?



