1个Statement 执行多条语句(除了用addBatch)应当怎么写呢?try{
Connection con = DriverManager.getConnection(
  "jdbc:mysql://localhost/jspdev?user=root&password=root&allowMultiQueries=true");Statement stmt = con.createStatement();String sql1 = new String("use " + database + "; select * from " + table);
String sql2 = new String("use " + database + "; select * from " + table);ResultSet rst1 = stmt.executeQuery(sql1);
ResultSet rst2 = stmt.executeQuery(sql2);}catch{
..........
}例如2条sql放到2个ResultSet  我的报错了

解决方案 »

  1.   

    insert update 还有delete的话可以拼起来一起执行你这个查询 还是得分开
      

  2.   

    2个Statement 只需一个Connection 怎么写?
      

  3.   


    你可以把生成connection的那段code提成一个单独的方法, 放到构造方法或者静态域或者非静态域中,只要保证它只被执行一次就可以了。然后在你的这个方法中,取得已经实例好的connection,再调用conn取得statement就可以了。
      

  4.   

    Statement stmt1 = con.createStatement();
    Statement stmt2 = con.createStatement();
      

  5.   

    我这样用木有错。Connection con;
    Statement stmt1;
    Statement stmt2; 
    ResultSet rst1;
    ResultSet rst2;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/db?user=root&password=123456&allowMultiQueries=true");

    stmt1 = con.createStatement();
    stmt2 = con.createStatement(); String table = "admin";
    String sql1 = new String("select * from " + table);
    String sql2 = new String("select * from " + table); rst1 = stmt1.executeQuery(sql1);
    rst2 = stmt1.executeQuery(sql2);

    } catch (SQLException e) {
    .....
    } catch (ClassNotFoundException e) {
    .....
    }finally{
    .....
    }