java 调用存储过程无反应_java调用存储过程无法取得返回参数
環境:數據庫sql server2005,jdk1.6 ,myeclipse,驅動jdts1.2.2
執行以下代碼,報錯:
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)
報錯信息說得很明白,就是輸出結果參數未處理,必須調用getMoreResults()方法以判斷是否還有結果集。
然后修改代碼,問題解決:
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記錄集循環取出后或者調用cstmt.getMoreResults()方法后,sqlserver才會處理output返回值
*/
if (!cstmt.getMoreResults()) {//此行判斷是否還有更多的結果集,如果沒有,接下來會處理output返回參數了
temp = cstmt.getString(5);//此行不再報錯
}
其中改為以下代碼也不報錯:
if (rs != null) {
總結
以上是生活随笔為你收集整理的java 调用存储过程无反应_java调用存储过程无法取得返回参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 激励机制的设计
- 下一篇: Ubutu 12.04LTS 安装搜狗拼