Given the code fragment:

Connection conn = DriverManager.getConnection(dbURL, userName, passWord);
String query = "SELECT id FROM Employee";
try (Statement stmt = conn.createStatement()) {
    ResultSet rs = stmt.executeQuery(query);
    stmt.executeQuery("SELECT id FROM Customer");
    while (rs.next()) {
        //process the results
        System.out.println("Employee ID: " + rs.getInt("id"));
    }
} catch (Exception e) {
    System.out.println("Error");
}

Assume that:

The required database driver is configured in the classpath.
The appropriate database is accessible with the dbURL, userName, and passWord exists.
The Employee and Customer tables are available and each table has id column with a few records and the SQL queries are valid.

What is the result of compiling and executing this code fragment?

A. The program prints employee IDs.
B. The program prints customer IDs.
C. The program prints Error.
D. compilation fails on line 13.

题解

程序第 13 行,又用了一次 Statement 对象的 executeQuery 方法,会使得先前的 ResultSet 自动被关闭,因此程序第 14 行会抛出 SQLException。