CallableStatement获得存储过程多个结果集

这里使用到的数据库为MySQL

package com.dz.entity;

import java.sql.*;

public class Pro_inoutTest {
    public static void main(String[] args) {
        insertDataByProcedure();
    }

    public static void insertDataByProcedure() {
        Connection conn = null;
        CallableStatement cs = null;
        try {
            //得到连接
            conn = DBUtil.getConnection();
            //调用哪个存储过程prepareCall
            cs = conn.prepareCall("{call pp3(?, ?)}");
            //这里的两个参数为inout(输入输出参)
            cs.setString(1,"333");
            cs.setString(2,"egy49xa");
            //将指定序号位置的 OUT 参数注册为给定的 JDBC 类型。
            cs.registerOutParameter(1, Types.VARCHAR);
            cs.registerOutParameter(2, Types.VARCHAR);
            boolean hasMore = cs.execute();
            //取指定位置的值
            System.out.println(cs.getObject(1));
            System.out.println(cs.getObject(2));
            while(true){
                //首先判断是否是结果集
                if(hasMore){
                    ResultSet rs = cs.getResultSet();
                    while(rs.next()){
                        System.out.println(rs.getObject(1));
                    }
                }else{
                    int count = cs.getUpdateCount();
                    if(count == -1){
                        //当count为-1时则结果集为最后一条数据
                        System.out.println("最后一条数据");
                        break;
                    }
                }
                hasMore = cs.getMoreResults();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
           DBUtil.close();
        }
    }
}
package com.dz.entity;

import java.sql.*;

public class Pro_inoutTest {
public static void main(String[] args) {
insertDataByProcedure();
}

public static void insertDataByProcedure() {
Connection conn = null;
CallableStatement cs = null;
try {
//得到连接
conn = DBUtil.getConnection();
//调用哪个存储过程prepareCall
cs = conn.prepareCall("{call pp3(?, ?)}");
//这里的两个参数为inout(输入输出参)
cs.setString(1,"333");
cs.setString(2,"egy49xa");
//将指定序号位置的 OUT 参数注册为给定的 JDBC 类型。
cs.registerOutParameter(1, Types.VARCHAR);
cs.registerOutParameter(2, Types.VARCHAR);
boolean hasMore = cs.execute();
//取指定位置的值
System.out.println(cs.getObject(1));
System.out.println(cs.getObject(2));
while(true){
//首先判断是否是结果集
if(hasMore){
ResultSet rs = cs.getResultSet();
while(rs.next()){
System.out.println(rs.getObject(1));
}
}else{
int count = cs.getUpdateCount();
if(count == -1){
//当count为-1时则结果集为最后一条数据
System.out.println("最后一条数据");
break;
}
}
hasMore = cs.getMoreResults();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close();
}
}
}
原文地址:https://www.cnblogs.com/dzcici/p/9676471.html