如何通过jdbc调用存储过程

2023-07-06   


在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


相关内容:

  1. PHP如何调用MYSQL存储过程
  2. Ibatis如何调用存储过程
  3. Jdbc数据访问技术面试题
  4. 在DELPHI中调用存储过程和使用内嵌SQL哪种方式更好
  5. 在DELPHI中调用存储过程和使用内嵌SQL哪种方式更好
  6. 存储过程和函数的区别