1. Call a stored procedure that returns a result set: a stored procedure that returns results by calling "Execute SQL Statement"
Take a recently used business as an example. The basic business is like this: Find all suppliers that have both equipment A and equipment B.
Stored procedure:
--exec getSupplierInfo2 12, 'distribution monitoring and control terminal'; test one --exec getSupplierInfo2 12, 'AC10kV power cable'; test two CREATE PROCEDURE [dbo].[getSupplierInfo2] @itemID int,--item number @deviceType varchar(100)--device type AS BEGIN SET NOCOUNT ON; declare @count int;--the number of models declare @sql nvarchar(max) --1. Obtain the number of equipment codes according to the item serial number and equipment category, and see how many models the supplier needs to meet select @count = count(distinct(field6)) from table7 where field2=@itemID and field7=@deviceType and field6 is not null; --print @count; if(@count<=0)--indicates that there is no qualified begin set @sql='select 0'; end else begin if(@count=1)--There is only one kind, so you can query directly 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--indicates that there are many kinds, then one kind of loop declare @c int;--cycle use set @c = 1; set @sql=''; while(@c < = @count) begin declare @field6 varchar(200);--device encoding --2, get the device code 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 'Device code: '+@field6; --3. Obtain the serial number of the supplier 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 code call:
/** * This method is used to obtain the serial number of qualified suppliers * @param itemID: item number * @param devicesType: device type * @return Object: 0-No qualified supplier data null-If there is an exception, you can check it through the log file String: Supplier serial number string, separated by commas * **/ public Object getSuppliers(int itemID,String devicesType){ StringBuffer sql = new StringBuffer(); CachedRowSetImpl rs = null; String result = ""; try{ //Get the supplier serial number that meets the conditions 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("Get supplier serial number information (device type: "+devicesType+" program serial number: "+itemID+")", sql.toString(),result); }catch(Exception e){ writeLoggerForException("Get supplier serial number information (device type: "+devicesType+" program serial number: "+itemID+")", sql.toString(), e); e.printStackTrace (); return null; }finally{ utils.closeCachedRowSet(rs); } return result; } /** * This method is used to execute query sql: use CachedRowSetImpl to query * @param sql: the sql to execute the query * @return CachedRowSetImpl: result set * */ 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. Call a stored procedure that does not return a result
Stored procedure:
CREATE PROCEDURE [dbo].[setSupplier] @programID int,--program number, primary key number of table6 @supplierID int -- the service design unit serial number, the primary key serial number of table18 AS BEGIN SET NOCOUNT ON; --1, get the identification value declare @field40 int ;--Identifies the value select @field40 = field40 from table6 where field1=@programID; if(@field40=0)--is 0, then it needs to be modified begin --Get the maximum identification value of all eligible scheme numbers under the frame selection list number where the current scheme is located select @field40 = max(field40)+1 from table6 where field41=(select top 1 field41 from table6 where field1=@programID) end declare @supplierName varchar(200);--Service Design Manufacturer 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 code call:
/** * This method is used to execute the stored procedure * @param programID: program number * @param supplierID: Design unit serial number * **/ 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();//Execute result = 1; } }catch(Exception e){ e.printStackTrace (); result = 0; }finally{ com.tzj.hl.db.HlBaseDao.closeConn(null,callStmt,conn); } return result; }