您的位置:首页 > 其它

PLS-00201: 必须声明标识符 'PKG_CONST.R_CURSOR'

2017-10-10 16:48 1226 查看
一、sql语句:

CREATE TABLE person (
id NUMBER(11) NOT NULL ,
username VARCHAR2(255 ) NULL ,
age NUMBER(11) NULL ,
password VARCHAR2(255) NULL ,
PRIMARY KEY (id)

)

自行插入一些数据;

二、创建查询所有记录(列表)的存储过程如下:

create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is 

begin 
open p_cursor for select * from person; 
exception 
when others then 
dbms_output.put_line('获取信息发生错误');

end pro_person_findall;

报错:

PROCEDURE NEB.PRO_PERSON_FINDALL 编译错误

错误:PLS-00201: 必须声明标识符 'PKG_CONST.R_CURSOR'

行:1

文本:create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is

错误:PL/SQL: Compilation unit analysis terminated

行:0

文本:create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is

解决方案:

由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分,

1, 建一个程序包。如下:

create or replace package testpackage as 

type test_cursor is ref cursor;

end testpackage;

2,建立存储过程,存储过程为:

create or replace procedure pro_person_findall(p_cursor out testpackage.test_cursor) is 

begin 
open p_cursor for select * from person; 
exception 
when others then 
dbms_output.put_line('获取信息发生错误');

end pro_person_findall;

JAVA代码:

import java.sql.*;

public class TestProcedureThreeFive {
public TestProcedureThreeFive() {}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@10.10.1.29:1521:testdb";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
//CallableStatement cstmt = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, "neb", "testneb");
CallableStatement proc = null; //创建执行存储过程的对象
proc = conn.prepareCall("{call pro_person_findall(?) }"); //设置存储过程 call为关键字.
//设置输出参数是一个游标.第一个参数,游标类型
proc.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
proc.execute();
rs = (ResultSet) proc.getObject(1);
while(rs.next()) {
System.out.println(rs.getString(1) + "," + rs.getString(2) + "," + rs.getInt(3) + "," + rs.getString(4));
}
}catch (SQLException ex2) {
ex2.printStackTrace();
}catch (Exception ex2) {
ex2.printStackTrace();
}finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex1) {
}
}
}
}
结果如下:
1,张三,22,123456

2,李四,23,123456

3,王五,24,123456
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  存储过程