java调用存储过程

1、调用有返回结果集的存储过程:通过调用“执行SQL语句”返回结果的存储过程

以最近用的一个业务为例子吧,基本业务是这样子的:查找既有设备A又有设备B的所有供货商。

存储过程:

--exec getSupplierInfo2 12,'配电测控终端';测试一个
--exec getSupplierInfo2 12,'AC10kV电力电缆';测试两个
CREATE PROCEDURE [dbo].[getSupplierInfo2]
	@itemID int,--项目序号
	@deviceType varchar(100)--设备类别
AS
BEGIN
	SET NOCOUNT ON;
	declare @count int;--型号数量
	declare @sql nvarchar(max)	

	--1、根据项目序号和设备类别获取设备编码的数量,看看供货商到底需要满足几种型号
	select @count = count(distinct(field6)) from table7 where field2=@itemID and field7=@deviceType and field6 is not null;
	--print @count;
	if(@count<=0)--说明没有符合条件的
	begin
		set @sql='select 0';
	end
	else
	begin
		if(@count=1)--只有一种,那么直接查询即可
		begin	
			set @sql = 'select distinct field9 from v_table14 where field2=(select top 1 field6 from table7 where field2='+cast(@itemID as varchar(20))+' and field7='''+@deviceType+''' and field6 is not null)';
		end
		else
		begin--说明有多种,那么一种一种循环
			declare @c int;--循环用
			set @c = 1;		
			set @sql='';
			while(@c < = @count)
			begin
				declare @field6 varchar(200);--设备编码
				--2、获取设备编码
				select @field6 = field6 from (
					select distinct field6,row_number() over(order by field1) as fn from table7 where field2=@itemID and field7=@deviceType and field6 is not null ) as a
				where fn=@c;
				--print '设备编码:'+@field6;

				--3、获取供货商序号
				set @sql=@sql+' INTERSECT select distinct field9 from v_table14 where field2='''+@field6+'''';				
				set @c = @c+1;
			end
			set @sql = substring(@sql,12,len(@sql));					
		end
	end
	--print @sql;
	exec(@sql)
   
END

java代码调用:

/**
 * 该方法用来用来获取符合条件的供货商序号
 * @param itemID:项目序号
 * @param devicesType:设备类别
 * @return Object: 0-没有符合条件的供货商数据 null-出现异常可以通过日志文件进行排查  String:供货商序号字符串,以逗号隔开
 * **/
public Object getSuppliers(int itemID,String devicesType){
	StringBuffer sql = new StringBuffer();
	CachedRowSetImpl rs = null;
	String result = "";
	try{
		//获取符合条件的供货商序号
		sql.append("DECLARE @return_value int;EXEC @return_value = [dbo].[getSupplierInfo2] @itemID="+itemID+",@deviceType='"+devicesType+"';");
//			System.out.println("DECLARE @return_value int;EXEC @return_value = [dbo].[getSupplierInfo2] @itemID="+itemID+",@deviceType='"+devicesType+"';");
		rs = dbTools.execQuery(sql.toString());
		StringBuffer suppliers = new StringBuffer();
		while(rs.next()){
			suppliers.append(","+rs.getInt(1));
		}
		if(suppliers.length()>1)result = suppliers.substring(1);
		writeLoggerForSQL("获取供货商序号信息(设备类型:"+devicesType+" 方案序号:"+itemID+")", sql.toString(),result);
	}catch(Exception e){
		writeLoggerForException("获取供货商序号信息(设备类型:"+devicesType+" 方案序号:"+itemID+")", sql.toString(), e);
		e.printStackTrace();
		return null;
	}finally{
		utils.closeCachedRowSet(rs);
	}
	return result;
}
/**
 * 该方法用来执行查询sql:使用CachedRowSetImpl进行查询
 * @param sql:要执行查询的sql
 * @return CachedRowSetImpl:结果集合
 * */
public static CachedRowSetImpl execQuery(String sql){
	Connection conn=null;
	PreparedStatement pst=null;
	ResultSet rs = null;
	CachedRowSetImpl rowset = null;
	try{
		conn = com.tzj.hl.db.HlBaseDao.getConn();
		if(conn!=null){
			pst = conn.prepareStatement(sql);
			rowset=new CachedRowSetImpl();
			rowset.populate(pst.executeQuery());
		}
	}catch(Exception e){
		e.printStackTrace();
		return null;
	}finally{
		com.tzj.hl.db.HlBaseDao.closeConn(rs,pst,conn);
	}
	return rowset;
}

2、调用不返回结果的存储过程

存储过程:

CREATE PROCEDURE [dbo].[setSupplier]
	@programID int,--方案序号,table6的主键序号
	@supplierID int --服务设计单位序号,table18的主键序号	
AS
BEGIN	
	SET NOCOUNT ON;    
	--1、获取标识值
	declare @field40 int ;--标识值
	select @field40 = field40 from table6 where field1=@programID;
	if(@field40=0)--为0,那么需要修改
	begin
		--获取当前方案所在的框选清单编号下所有符合条件的方案号的最大的标识值
		select @field40 = max(field40)+1 from table6 where field41=(select top 1 field41 from table6 where field1=@programID) 
	end
	declare @supplierName varchar(200);--服务设计厂商
	declare @pay decimal(16,2);	
	select @supplierName = field4,@pay=field11 from table_cfb_pj_unit_info_view where field1=@supplierID;
	update table6 set field29=getDate(),field27=@supplierID,field28=@supplierName,field40 = @field40,field36=@pay 
	where field1=@programID;
END

java代码调用:

/**
 * 该方法用来执行存储过程
 * @param programID:方案序号
 * @param supplierID:设计单位序号
 * **/
public static int execCallableQuery(int programID,int supplierID){
	Connection conn=null;
	CallableStatement  callStmt =null;
	int result=0;
	try{
		conn = com.tzj.hl.db.HlBaseDao.getConn();
		if(conn!=null){
			callStmt  = conn.prepareCall("{call setSupplier(?,?)}");
			callStmt.setInt(1,programID);
			callStmt.setInt(2,supplierID);
			callStmt.execute();//执行
			result = 1;
		}
	}catch(Exception e){
		e.printStackTrace();
		result = 0;
	}finally{
		com.tzj.hl.db.HlBaseDao.closeConn(null,callStmt,conn);
	}
	return result;
}

 

你可能感兴趣的:(Java,SQL,Server)