Let Java return multiple resultset from MySQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.