// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

// -Djava.library.path=C:\DivJava\sqljdbc_4.0\enu\auth\x64
public class Multi {
    public static void testExecuteQuery(String driver, String conurl, String un, String pw) throws SQLException, ClassNotFoundException {
        Class.forName(driver);
        try(Connection con = DriverManager.getConnection(conurl, un, pw)) {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM T1;SELECT * FROM T1");
            while(rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
            rs.close();
            stmt.getMoreResults();
            rs = stmt.getResultSet();
            while(rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
            rs.close();
            stmt.close();
        }
    }
    public static void testExecute(String driver, String conurl, String un, String pw) throws SQLException, ClassNotFoundException {
        Class.forName(driver);
        try(Connection con = DriverManager.getConnection(conurl, un, pw)) {
            Statement stmt = con.createStatement();
            stmt.execute("SELECT * FROM T1;SELECT * FROM T1");
            ResultSet rs = stmt.getResultSet();
            while(rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
            rs.close();
            stmt.getMoreResults();
            rs = stmt.getResultSet();
            while(rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
            rs.close();
            stmt.close();
        }
    }
    public static void setup(String driver, String conurl, String un, String pw) throws SQLException, ClassNotFoundException {
        Class.forName(driver);
        try(Connection con = DriverManager.getConnection(conurl, un, pw)) {
            Statement stmt = con.createStatement();
            stmt.execute("CREATE TABLE t1 (f1 INTEGER NOT NULL, f2 VARCHAR(50), PRIMARY KEY(f1))");
            stmt.execute("INSERT INTO t1 VALUES(1,'A')");
            stmt.execute("INSERT INTO t1 VALUES(2,'BB')");
            stmt.execute("INSERT INTO t1 VALUES(3,'CCC')");
            stmt.close();
        }
    }
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //testExecuteQuery("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/Test", "", "");
        //testExecute("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/Test", "", "");
        testExecuteQuery("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/Test?allowMultiQueries=true", "", "");
        testExecute("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/Test?allowMultiQueries=true", "", "");
        //testExecuteQuery("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@localhost:1521:xe", "arne", "hemmeligt");
        //testExecute("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@localhost:1521:xe", "arne", "hemmeligt");
        //testExecuteQuery("com.ibm.db2.jcc.DB2Driver", "jdbc:db2://localhost:50000/Test", "arne", "hemmeligt");
        //testExecute("com.ibm.db2.jcc.DB2Driver", "jdbc:db2://localhost:50000/Test", "arne", "hemmeligt");
        //setup("org.h2.Driver", "jdbc:h2:file:/work/h2test", "sa", "");
        //testExecuteQuery("org.h2.Driver", "jdbc:h2:file:/work/h2test", "sa", "");
        //testExecute("org.h2.Driver", "jdbc:h2:file:/work/h2test", "sa", "");
        testExecuteQuery("com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://localhost;database=Test;integratedSecurity=true;", null, null);
        testExecute("com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://localhost;database=Test;integratedSecurity=true;", null, null);
        //testExecuteQuery("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:/work/derbytest;create=true", null, null);
        //testExecute("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:/work/derbytest;create=true", null, null);
    }
}
