Posted in 面试题 onApril 19, 2012
在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
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
如何通过jdbc调用存储过程
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@