I am trying to retrieve a resultset from a procedure in Java but I'm unable to do so. Although usually I easily retrieve resultset this time a null pointer exception is encountered.
Here is my sql server 2008 proceedure:
ALTER PROCEDURE [dbo].[SSSMLTransaction]
@acid int,@subacid int
AS
BEGIN
CREATE TABLE trans ( Vtype varchar(10),Vno varchar(5),Vdate date,Narr varchar(50),dr numeric(13,2),cr numeric(13,2),DCIND varchar(1));
insert into trans(Vtype,Vno,Vdate,Narr,dr,cr,DCIND)
(select 'Cash',cd.V_no,cv.VDate,cv.Narr1,cd.Debit,cd.Credit,cd.DCIND from CVDetail cd join CashVoucher cv on cd.V_no=cv.Vno where
cd.ANO=@acid and cd.Party_Code=@subacid) ;
select * from trans;
drop table trans;
END
And here is my Java function:
CallableStatement cs = conn.prepareCall("{call SSSMLTransaction(?,?)}");
cs.setInt(1, acid);
cs.setInt(2, prtyid);
cs.execute();
rs=cs.getResultSet();
But I got a empty resultset.
尝试:
ResultSet rs = cs.executeQuery();
Best way to retrieve a data from Procedure is as follows :
public static void executeProcedure(Connection con) {
try {
CallableStatement stmt = con.prepareCall(...);
..... //Set call parameters, if you have IN,OUT, or IN/OUT parameters
boolean results = stmt.execute();
int rsCount = 0;
//Loop through the available result sets.
while (results) {
ResultSet rs = stmt.getResultSet();
//Retrieve data from the result set.
while (rs.next()) {
....// using rs.getxxx() method to retieve data
}
rs.close();
//Check for next result set
results = stmt.getMoreResults();
}
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
In your case , i suppose there is no data to return based on your SQL query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.