java中呼叫儲存過程

weixin_34304013發表於2012-08-20

資料庫中的儲存過程

CREATE PROCEDURE [dbo].[Proc_deleteStuByNo]
 -- Add the parameters for the stored procedure here
 @stuNo char(7),
 @result int output
AS
BEGIN
 SET NOCOUNT ON;
 delete from studinfo where StudNo=@stuNo
 set @result=@@rowcount
END

 

java中呼叫儲存過程:

String stuno="a1";

int result=0;

Connection connection = ConnDB.getConection();
   CallableStatement callStm=null;
   try {
    callStm = connection.prepareCall("{call Proc_deleteStuByNo(?,?)}");
    callStm.setString("stuNo",stuno);//為CallableStatement物件新增輸入引數
    callStm.registerOutParameter("result",java.sql.Types.INTEGER);//為CallableStatement物件註冊輸出引數  

 } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    callStm.executeUpdate();
    result = callStm.getInt("result");//獲取輸出引數中的值
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally{
    try {
     callStm.close();
     connection.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }   
   }
   if(result>0){
    System.out.println("刪除成功");
   }else{

  System.out.println("刪除失敗");
    }

相關文章