<?xml version="1.0" encoding="UTF-8"?><!-- generator="bbPress" -->

<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>

<channel>
<title>~SingleForum~无废话记录~ Topic: 存储过程返回结果集</title>
<link>http://www.ll19.com/bb/</link>
<description>~19~记录点有用的好吗？少说废话少装b~</description>
<language>en</language>
<pubDate>Tue, 07 Feb 2012 08:58:26 +0000</pubDate>

<item>
<title>admin on "存储过程返回结果集"</title>
<link>http://www.ll19.com/bb/topic/93#post-93</link>
<pubDate>Sun, 03 Jan 2010 15:50:08 +0000</pubDate>
<dc:creator>admin</dc:creator>
<guid isPermaLink="false">93@http://www.ll19.com/bb/</guid>
<description>&#60;p&#62;首先要明确一个概念&#60;br /&#62;
PROCEDURE  TRIGGER PACKAGE 的区别&#60;br /&#62;
简单的说，PROCEDURE, TRIGGER都是一段程序&#60;br /&#62;
但PROCEDURE需要手工调用，TRIGGER满足你设定的条件会自动执行&#60;br /&#62;
PACKAGE是 把几个程序封装在一起，和JAVA里的PACKAGE的概念差不多。&#60;/p&#62;
&#60;p&#62;得到返回的结果集需要建立PACKAGE，并且返回列表需要通过返回游标实现&#60;/p&#62;
&#60;p&#62;由于oracle存储过程没有返回值，它的所有返回值都是通过out参数来替代的，列表同样也不例外，但由于是集合，所以不能用一般的参数，必须要用pagkage了.所以要分两部分。&#60;/p&#62;
&#60;p&#62;1、在SQL*PLUS中建一个程序包（在包中定义了一个类型游标）&#60;/p&#62;
&#60;p&#62;CREATE OR REPLACE PACKAGE TESTPACKAGE AS&#60;br /&#62;
TYPE Test_CURSOR IS REF CURSOR;&#60;br /&#62;
procedure TESTC(cur_ref out Test_CURSOR);&#60;br /&#62;
end TESTPACKAGE;&#60;/p&#62;
&#60;p&#62;2、建立包之后建立存储过程 &#60;/p&#62;
&#60;p&#62;create or replace package body TESTPACKAGE as&#60;br /&#62;
procedure TESTC(cur_ref out Test_CURSOR) is&#60;br /&#62;
begin&#60;br /&#62;
OPEN cur_ref FOR select * from test;&#60;br /&#62;
end TESTC;&#60;br /&#62;
END TESTPACKAGE;&#60;/p&#62;
&#60;p&#62;可以看到，它是把游标(cursor)（可以理解为一个指针），作为一个out 参数来返回值的。&#60;/p&#62;
&#60;p&#62;JAVA调用程序如下：&#60;/p&#62;
&#60;p&#62;<div class="igBar"><span id="ljava-1"><a href="#" onclick="javascript:showPlainTxt('java-1'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JAVA:</span><br /><div id="java-1">
<div class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">package com.<span style="color: #006600;">rizon</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">excelApi</span>;&amp;#60;/p&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;<span style="color: #a1a100;">import java.sql.Connection;&amp;#60;br /&amp;#62;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import java.sql.DriverManager;&amp;#60;br /&amp;#62;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import java.sql.ResultSet;&amp;#60;br /&amp;#62;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import java.sql.SQLException;&amp;#60;/p&amp;#62;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;public <span style="color: #000000; font-weight: bold;">class</span> CursorTest <span style="color: #66cc66;">&#123;</span>&amp;#60;/p&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;&nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?q=allinurl%3AClassNotFoundException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassNotFoundException</span></a>,&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASQLException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">SQLException</span></a> <span style="color: #66cc66;">&#123;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AConnection+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Connection</span></a> conn = <span style="color: #000000; font-weight: bold;">null</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AResultSet+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ResultSet</span></a> rs = <span style="color: #000000; font-weight: bold;">null</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>&amp;#60;/p&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006600;">forName</span><span style="color: #66cc66;">&#40;</span>&amp;#34;oracle.<span style="color: #006600;">jdbc</span>.<span style="color: #006600;">driver</span>.<span style="color: #006600;">OracleDriver</span>&amp;#34;<span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn = <a href="http://www.google.com/search?q=allinurl%3ADriverManager+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">DriverManager</span></a>.<span style="color: #006600;">getConnection</span><span style="color: #66cc66;">&#40;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;#34;jdbc:oracle:thin:@<span style="color: #cc66cc;color:#800000;">10</span>.<span style="color: #cc66cc;color:#800000;">0</span>.<span style="color: #cc66cc;color:#800000;">41</span>.<span style="color: #cc66cc;color:#800000;">100</span>:<span style="color: #cc66cc;color:#800000;">1521</span>:orcl&amp;#34;, &amp;#34;forestrytree&amp;#34;,&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;#34;forestrytree&amp;#34;<span style="color: #66cc66;">&#41;</span>;&amp;#60;/p&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;&nbsp;&nbsp;&nbsp;oracle.<span style="color: #006600;">jdbc</span>.<span style="color: #006600;">OracleCallableStatement</span> stmt = <span style="color: #66cc66;">&#40;</span>oracle.<span style="color: #006600;">jdbc</span>.<span style="color: #006600;">OracleCallableStatement</span><span style="color: #66cc66;">&#41;</span> conn&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006600;">prepareCall</span><span style="color: #66cc66;">&#40;</span>&amp;#34;<span style="color: #66cc66;">&#123;</span>call TESTPACKAGE.<span style="color: #006600;">TESTC</span><span style="color: #66cc66;">&#40;</span>?<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>&amp;#34;<span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// stmt.setString(1, &amp;#34;1&amp;#34;);&amp;#60;br /&amp;#62;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmt.<span style="color: #006600;">registerOutParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">1</span>, oracle.<span style="color: #006600;">jdbc</span>.<span style="color: #006600;">OracleTypes</span>.<span style="color: #006600;">CURSOR</span><span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmt.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs = stmt.<span style="color: #006600;">getCursor</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;color:#800000;">0</span>; rs.<span style="color: #006600;">next</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>&amp;#34; ----<span style="color: #66cc66;">&#91;</span>&amp;#34; + i + &amp;#34;<span style="color: #66cc66;">&#93;</span>name:&amp;#34; + rs.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + &amp;#34;telephone:&amp;#34; + rs.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> ex<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #66cc66;">&#123;</span>&amp;#60;/p&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>conn != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.<span style="color: #006600;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn = <span style="color: #000000; font-weight: bold;">null</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&amp;#60;/p&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;&nbsp;&nbsp;&nbsp;<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>rs != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs.<span style="color: #006600;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs = <span style="color: #000000; font-weight: bold;">null</span>;&amp;#60;br /&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&amp;#60;br /&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&amp;#60;/p&amp;#62;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;&nbsp;<span style="color: #66cc66;">&#125;</span>&amp;#60;/p&amp;#62;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&amp;#60;p&amp;#62;<span style="color: #66cc66;">&#125;</span>&amp;#60;br /&amp;#62; </div></li></ol></div>
</div></div><br />
&#60;/p&#62;</description>
</item>

</channel>
</rss>

