疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java中调用存储过程


 

 java中调用存储过程

 
数据库中的存储过程
 
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("删除失败");
    }