首先,JDBC對于SQLSERVER來說預設是支援傳回,但對于MySql來說,隻預設支援存儲過程傳回多個ResultSet,那對于手寫SQL怎麼辦。
其實很簡單,隻要一個在連接配接字元串中加一個參數:allowMultiQueries=true。
代碼實作如下:
@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();
}
本文基于
Creative Commons Attribution 2.5 China Mainland License釋出,歡迎轉載,演繹或用于商業目的,但是必須保留本文的署名
http://www.cnblogs.com/luminji(包含連結)。如您有任何疑問或者授權方面的協商,請給我留言。