环境:数据库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) { 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);//此行不再报错 //}