微信

使用微信服务,更方便

职友集>Java面试题 > 如何通过jdbc调用存储过程

如何通过jdbc调用存储过程

2015-11-01 06:30:01 阅读( 2 )

2682人 收藏本页

标签:Java面试题

在java.sql.Connection类中的public CallableStatement prepareCall(String sql)方法调用存储过程。Sql为an SQL statement that may contain one or more ‘?’ parameter placeholders. Typically this statement is a JDBC function call escape string.,即调用存储过程语句
Example:
Stored procedures:
Create or replace procedure addMember(name in varchar2(50),sex in varchar2(50)) as
begin
insert into family f (f.name,f.sex) values (name,sex);
end addMember;
Java source code:
…………………………………get Connection
String procedure = “{call addMember(?,?)}”;
CallableStatement cstmt = conn.prepareCall(procedure);
cstmt.setString(1,”马锐”);
cstmt.serString(2,”男”);
cstmt.excute();
………..close connection

若是需要返回值,我们可以通过function实现。可用下面方法
Example:
Stored procedures:
Create or replace function viewMember return types.cursortype as
family_cursor types.cursortype;
begin
open family_cursor for select f.name,f.sex from family f
return family_cursor
end addMember;
Java source code:
…………………………………get Connection
String procedure =”begin :1:=viewMember;end;”;
CallableStatement cstmt = conn.prepareCall(procedure);
cstmt.registerOutParameter(1,OracleTypes.CURSOR);
cstmt.execute();
ResultSet rset = (ResultSet)cstmt.getObject(1);
while(rset.next())
System.out.println(rset.getString(1);
cstmt.close();
)
………..close connection

来自IT公司面试手册

下一篇:线程间的同步方法

上一篇:J2EE面试题集锦(附答案)

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息: 面试题