问题描述:有时使用JDBC进行数据库查询时会返回多个结果集,但是PreparedStatement
方式只能接收第一个结果集。
例如在SQL Server中执行sp_help 'test.student'
,该语句会返回至少7个结果集。
1、使用PreparedStatement
进行JDBC操作
public class MSSqlTest {
public static void main(String[] args) {
System.out.println(getOneResultSet());
}
public static List<Map> getOneResultSet() {
List<Map> result = new ArrayList<>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://192.168.14.172:11433;" +
"databaseName=test;instanceName=test", "sa", "FFCSffcs8!");
// 该语句会返回多个结果集
pstmt = conn.prepareCall("sp_help 'test.student'");
rs = pstmt.executeQuery();
while (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Map map = new HashMap();
for (int i = 0; i < columnCount; i++) {
map.put(rsmd.getColumnName(i + 1).toLowerCase(), rs.getObject(i + 1));
}
result.add(map);
}
}catch (Exception e) {
System.out.println(e.getMessage());
}finally{
if(null !=rs ){
try {
rs.close();
} catch (SQLException ex) {
}
}
if(null != pstmt){
try {
pstmt.close();
} catch (SQLException ex) {
}
}
if(null != conn){
try {
conn.close();
} catch (SQLException ex) {
}
}
}
return result;
}
}
调用该方法只会返回第一个结果集的内容:
[{owner=dbo, name=student, type=user table, created_datetime=2019-01-08 06:53:33.163}]
2、使用CallableStatement
进行JDBC操作
public class MSSqlTest {
public static void main(String[] args) {
int resultCount = 0;
for (List<Map> maps : getMoreResultSet()) {
System.out.println("结果集" + resultCount++ + " : ");
System.out.println(JSON.toJSONString(maps));
}
}
public static List<List<Map>> getMoreResultSet() {
List<List<Map>> resultList = new ArrayList<>();
Connection conn = null;
CallableStatement cstmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://192.168.14.172:11433;" +
"databaseName=test;instanceName=test", "sa", "FFCSffcs8!");
// 该语句会返回多个结果集
cstmt = conn.prepareCall("sp_help 'test.student'");
rs = cstmt.executeQuery();
boolean oprFlg = cstmt.execute();
// 外循环获取结果集的个数
while (oprFlg) {
List<Map> result = new ArrayList<>();
rs = cstmt.getResultSet();
// 内循环获取每个结果集的记录
while (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Map map = new HashMap();
for (int i = 0; i < columnCount; i++) {
map.put(rsmd.getColumnName(i + 1).toLowerCase(), rs.getObject(i + 1));
}
result.add(map);
}
resultList.add(result);
oprFlg = cstmt.getMoreResults();
}
}catch (Exception e) {
System.out.println(e.getMessage());
}finally{
if(null !=rs ){
try {
rs.close();
} catch (SQLException ex) {
}
}
if(null != cstmt){
try {
cstmt.close();
} catch (SQLException ex) {
}
}
if(null != conn){
try {
conn.close();
} catch (SQLException ex) {
}
}
}
return resultList;
}
}
此时便可以获取所有结果集:
结果集0 :
[{"owner":"dbo","name":"student","type":"user table","created_datetime":1546901613163}]
结果集1 :
[{"trimtrailingblanks":"(n/a)","fixedlennullinsource":"(n/a)","computed":"no","prec":"10 ","nullable":"no","column_name":"c_id","length":4,"scale":"0 ","type":"int"},{"trimtrailingblanks":"no","fixedlennullinsource":"yes","computed":"no","prec":" ","nullable":"yes","column_name":"c_name","length":200,"scale":" ","collation":"SQL_Latin1_General_CP1_CI_AS","type":"varchar"},{"trimtrailingblanks":"(n/a)","fixedlennullinsource":"(n/a)","computed":"no","prec":"10 ","nullable":"no","column_name":"c_age","length":4,"scale":"0 ","type":"int"},{"trimtrailingblanks":"(n/a)","fixedlennullinsource":"(n/a)","computed":"no","prec":"10 ","nullable":"yes","column_name":"s_id","length":4,"scale":"0 ","type":"int"}]
结果集2 :
[{"identity":"No identity column defined."}]
结果集3 :
[{"rowguidcol":"No rowguidcol column defined."}]
结果集4 :
[{"data_located_on_filegroup":"PRIMARY"}]
结果集5 :
[{"index_description":"clustered, unique, primary key located on PRIMARY","index_keys":"c_id","index_name":"PK__student__213EE774F2EB6C88"}]
结果集6 :
[{"update_action":"(n/a)","constraint_type":"DEFAULT on column c_age","status_for_replication":"(n/a)","constraint_name":"DF__student__c_age__440B1D61","delete_action":"(n/a)","status_enabled":"(n/a)","constraint_keys":"((18))"},{"update_action":"(n/a)","constraint_type":"DEFAULT on column c_name","status_for_replication":"(n/a)","constraint_name":"DF__student__c_name__4316F928","delete_action":"(n/a)","status_enabled":"(n/a)","constraint_keys":"('jack')"},{"update_action":"(n/a)","constraint_type":"PRIMARY KEY (clustered)","status_for_replication":"(n/a)","constraint_name":"PK__student__213EE774F2EB6C88","delete_action":"(n/a)","status_enabled":"(n/a)","constraint_keys":"c_id"}]
结果集7 :
[{"table is referenced by foreign key":"TEST.test.test: FK__test__s_id__6D0D32F4"}]