分享

java调用存储过程无法取得返回参数

 sam225 2010-04-15

java调用存储过程无法取得返回参数 收藏
环境:数据库sql server2005,jdk1.6 ,myeclipse,驱动jdts1.2.2

执行以下代码,报错:

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)

报错信息说得很明白,就是输出结果参数未处理,必须调用getMoreResults()方法以判断是否还有结果集。

然后修改代码,问题解决:

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记录集循环取出后或者调用cstmt.getMoreResults()方法后,sqlserver才会处理output返回值 
*/ 
if (!cstmt.getMoreResults()) {//此行判断是否还有更多的结果集,如果没有,接下来会处理output返回参数了  
    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记录集循环取出后或者调用cstmt.getMoreResults()方法后,sqlserver才会处理output返回值
*/
if (!cstmt.getMoreResults()) {//此行判断是否还有更多的结果集,如果没有,接下来会处理output返回参数了
 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博客,转载请标明出处:http://blog.csdn.net/security08/archive/2010/01/08/5148586.aspx

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约