scvmm 发表于 2018-10-8 08:51:29

让java从Mysql返回多个ResultSet-13166875

  @Test

  public void test01() throws SQLException,>  String DBDRIVER = "com.mysql.jdbc.Driver";
  String DBURL = "jdbc:mysql://localhost:3306/bookstore?allowMultiQueries=true";
  String DBUSER = "root";
  String DBPASS = "root";
  Connection conn = null;

  >  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();
  }

页: [1]
查看完整版本: 让java从Mysql返回多个ResultSet-13166875