在下面的实例中,将向此函数传递 SQL Server AdventureWorks 示例数据库的打开连接,并构造一条 SQL 语句,该语句在运行后将返回两个结果集:
public static void executeStatement(Connection con) {
try {
String SQL = "SELECT TOP 10 * FROM Person.Contact; _" +
"SELECT TOP 20 * FROM Person.Contact";
Statement stmt = con.createStatement();
boolean results = stmt.execute(SQL);
int rsCount = 0;
//Loop through the available result sets.
do {
if(results) {
ResultSet rs = stmt.getResultSet();
rsCount++;
//Show data from the result set.
System.out.println("RESULT SET #" + rsCount);
while (rs.next()) {
System.out.println(rs.getString("LastName") + ", " + rs.getString("FirstName"));
}
rs.close();
}
System.out.println();
results = stmt.getMoreResults();
} while(results);
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
本文介绍了一个Java示例,展示如何使用单条SQL语句从SQL Server数据库获取多个结果集,并遍历显示这些结果集中的数据。
26万+

被折叠的 条评论
为什么被折叠?



