java調(diào)用存儲過程無法取得返回參數(shù) 收藏
環(huán)境:數(shù)據(jù)庫sql server2005,jdk1.6 ,myeclipse,驅(qū)動jdts1.2.2
執(zhí)行以下代碼,報錯:
view plaincopy to clipboardprint?
String querySQL = "{?=call p_sys_manager_csReport(?,?,?,?,?)}";
cstmt = conn.prepareCall(querySQL);
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt.setInt(2, modType);
cstmt.setInt(3, dptId);
cstmt.setInt(4, eplId);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
rs = cstmt.executeQuery();
if (rs != null) {
if (rs.next()) {
companyTotal = rs.getInt("companyTotal");
}
}
String temp = null;
temp = cstmt.getString(5);//此行報錯
String querySQL = "{?=call p_sys_manager_csReport(?,?,?,?,?)}";
cstmt = conn.prepareCall(querySQL);
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt.setInt(2, modType);
cstmt.setInt(3, dptId);
cstmt.setInt(4, eplId);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
rs = cstmt.executeQuery();
if (rs != null) {
if (rs.next()) {
companyTotal = rs.getInt("companyTotal");
}
}
String temp = null;
temp = cstmt.getString(5);//此行報錯
報錯信息為:
java.sql.SQLException: Output parameters have not yet been processed. Call getMoreResults().
at net.sourceforge.jtds.jdbc.ParamInfo.getOutValue(ParamInfo.java:159)
at net.sourceforge.jtds.jdbc.JtdsCallableStatement.getOutputValue(JtdsCallableStatement.java:116)
at net.sourceforge.jtds.jdbc.JtdsCallableStatement.getString(JtdsCallableStatement.java:310)
報錯信息說得很明白,就是輸出結(jié)果參數(shù)未處理,必須調(diào)用getMoreResults()方法以判斷是否還有結(jié)果集。
然后修改代碼,問題解決:
view plaincopy to clipboardprint?
String querySQL = "{?=call p_sys_manager_csReport(?,?,?,?,?)}";
cstmt = conn.prepareCall(querySQL);
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt.setInt(2, modType);
cstmt.setInt(3, dptId);
cstmt.setInt(4, eplId);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
rs = cstmt.executeQuery();
if (rs != null) {
if(rs.next()) {
companyTotal = rs.getInt("companyTotal");
}
}
String temp = null;
/*
*記錄集獲取到后,把rs記錄集循環(huán)取出后或者調(diào)用cstmt.getMoreResults()方法后,sqlserver才會處理output返回值
*/
if (!cstmt.getMoreResults()) {//此行判斷是否還有更多的結(jié)果集,如果沒有,接下來會處理output返回參數(shù)了
temp = cstmt.getString(5);//此行不再報錯
}
String querySQL = "{?=call p_sys_manager_csReport(?,?,?,?,?)}";
cstmt = conn.prepareCall(querySQL);
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt.setInt(2, modType);
cstmt.setInt(3, dptId);
cstmt.setInt(4, eplId);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
rs = cstmt.executeQuery();
if (rs != null) {
if(rs.next()) {
companyTotal = rs.getInt("companyTotal");
}
}
String temp = null;
/*
*記錄集獲取到后,把rs記錄集循環(huán)取出后或者調(diào)用cstmt.getMoreResults()方法后,sqlserver才會處理output返回值
*/
if (!cstmt.getMoreResults()) {//此行判斷是否還有更多的結(jié)果集,如果沒有,接下來會處理output返回參數(shù)了
temp = cstmt.getString(5);//此行不再報錯
}
其中改為以下代碼也不報錯:
view plaincopy to clipboardprint?
if (rs != null) {
while(rs.next()) {//if改為while
companyTotal = rs.getInt("companyTotal");
}
}
String temp = null;
/*
* 去掉cstmt.getMoreResults(),將上面的if(rs.next()) 改為while(rs.next())也不報錯
*/
//if (!cstmt.getMoreResults()) {
temp = cstmt.getString(5);//此行不再報錯
//}
本文來自CSDN博客,轉(zhuǎn)載請標明出處:http://blog.csdn.net/security08/archive/2010/01/08/5148586.aspx