|
我想使用Java从数据库中检索数据并显示它,所以我创建了这个PL / SQL函数,该函数返回一个游标:
& v J4 P6 }/ }create or replace function std_getInfoFunc return types.cursortype 8 b; ^- T7 C# x0 w3 I! P
as + {5 d) G% E% C; M& {
my_cursor types.cursorType;
) D3 F$ d( Y7 A& s& nbegin 8 G+ q8 N9 x- Q3 {* W \. V8 _
open my_cursor FOR
; d& [9 }( D9 y SELECT s.FirstName, s.LastName, s.Address, s.City, s.Province
: S' V$ Y r! c , s.PostalCode, c.CourseName
# c& s! w# g: ~& x, T1 y# N# @ FROM Students s, Courses c, StudentAndCourses cs
" z, Z! s6 Q0 l& J9 C6 e Where s.StudentID = cs.StudentID
$ J0 e# q3 i0 `, ^) w, ` AND c.CourseID = cs.CourseID; j6 N' g* h4 Z1 [9 _1 w; _2 y
Return my_cursor;
& P9 n1 R# l, {/ S$ xend;
* t1 y! T! I1 o( L7 W在我的Java代码中,我按以下方式调用该函数:
8 v- F. e1 w3 G: b1 k: @& W2 ktry{. m$ N( }( V2 S8 X6 o" {. ^. o
CallableStatement cst=connection.prepareCall("{? = call std_getInfoFunc}");# h' |% y5 K4 @8 F1 p7 t
cst.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);6 j1 ], x" z6 Y* T+ Z
cst.execute();7 U0 X& n$ \* p5 D- o+ b; b7 H
res=(ResultSet) cst.getObject(1);
& M% _( ]) j/ k l/ h# L while(res.next()){. C& i7 _4 c( p9 r% X9 U6 J& X
System.out.println(res.getString(1));
( I" |6 V9 }! W( N7 m8 R }. y+ j+ s7 } R8 y
}
8 l1 m6 p% t$ [. c* _catch(SQLException e){
6 R) Y9 j5 q4 K4 s& n. B+ D K( K* A e.printStackTrace();$ S8 H; ~6 D$ `
}. M5 j Q' f: s: ?! F* a v; B- A
finally{
0 D5 l2 d# q6 c$ X" F res.close();% n( J/ |/ }4 A0 _$ S7 N3 S0 v
cst.close();, g! @4 z' k! C+ h* |! A2 Q
conn.close();, c: a: e+ R i5 r, |
}( |5 P9 `( k9 Q; ?. ?! @7 C
该代码产生以下异常:' B1 p# g# v& E8 {
run:4 r9 `9 l" s: p4 p
java.sql.SQLException: ORA-06550: line 1, column 13:
" ~3 F: ?& e! n+ e+ M PLS-00905: object SAS.STD_GETINFOFUNC is invalid6 p1 D' i$ P! M4 `+ z
ORA-06550: line 1, column 7:
' w5 d( g' O. u9 k' |+ P' n PL/SQL: Statement ignored
1 B7 _! x- M! C% u. R% y! J+ _2 y* hat oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
. o6 {- `% z2 C' cat oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)$ x! ~! J5 U: T) k. P! u* C* I8 ?
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
5 r$ n# F6 R' |( J) s% \, bat oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
' M9 @4 `: L3 Iat oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
( V9 \+ u# u; x/ y8 \at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
3 p+ U6 P% w+ v" {1 z7 @+ U2 z+ yat oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:202)
0 K; M2 Q" b* E! |at
) ?4 Z! c. B* c oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1005)
0 T3 p5 {1 j4 J% k3 l. A5 I. l$ Yat oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307). {! f2 Z/ y7 n( Y. D
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
7 c! r- }/ F* B: B- Jat oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3550)
, [- D R9 T3 y0 }% Dat oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4710)
! ?7 n% V4 w8 ~5 Dat oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)" W5 R" J( z! I/ ~# N+ Z- W( G, M
at DBControler.viewStdInfo(DBControler.java:95)
$ \; T5 w! X8 x: u$ Hat Test_02.main(Test_02.java:18). N2 C: T# L9 a. u9 D$ A
为什么?* Z Y" Y5 W! {3 a" s q7 e
, ]0 C" o+ Q4 x% J
解决方案:
, G |, g9 m9 n8 I# W
. v+ E5 }2 [ C1 `) \2 A( Y( k
4 S: R. Y+ X: J3 w' d* l3 p- K/ B+ K: E2 x4 H" ~3 @5 D3 d
错误的可能原因是:(在代码段-函数中)+ ^4 Y1 w4 J7 L1 i; F7 n, b! G
1.包“类型”不存在。
. o( a' U( q% z$ M: K create or replace package types % H, Z; C) K/ W5 ]& i0 z4 A* }' b
as
) _+ T& H4 Q$ w% _9 ]& ^, _ type cursorType is ref cursor; : p# k' [ {( g ]5 ]! _
end;
Z% Z, m& o6 t6 { k /
3 a9 Y9 ~) y0 D9 a2无效的SQL select语句(创建函数后已删除或更改表或列)。7 |+ ^/ J% j1 B& \% Z7 o% U
您需要编写一个new functionwith simplesql语句,并在Sql提示符下使用Java代码运行它。 |
|