First, JDBC is the default support for SQL Server to return, but for MySQL, only the default support stored procedures return multiple resultset, that for handwritten SQL.
It's really simple, as long as one parameter is added to the connection string: Allowmultiqueries=true.
The code is implemented as follows:
@Test
public void test01 () throws SQLException, ClassNotFoundException {
String dbdriver = "Com.mysql.jdbc.Driver";
String Dburl = "Jdbc:mysql://localhost:3306/bookstore?allowmultiqueries=true";
String DBUSER = "root";
String dbpass = "root";
Connection conn = null;
Class.forName (Dbdriver);
conn = Drivermanager.getconnection (Dburl, DBUSER, Dbpass);
SYSTEM.OUT.PRINTLN (conn);
String sql = "SELECT * from category;"
+ "SELECT * from user;";
Statement stmt = Conn.createstatement ();
Boolean isresultset = Stmt.execute (sql);
ResultSet rs = null;
int count = 0;
while (true) {
if (Isresultset) {
rs = Stmt.getresultset ();
while (Rs.next ()) {
System.out.println (rs.getstring (1));
}
Rs.close ();
} else {
if (stmt.getupdatecount () = =-1) {
Break
}
System.out.printf ("Result {} is just a count: {}", Count, Stmt.getupdatecount ());
}
Count + +;
Isresultset = Stmt.getmoreresults ();
}
Stmt.close ();
Conn.close ();
}
Let Java return multiple resultset from MySQL