Given the code fragment:
try {
String query = "SELECT * FROM Employee WHERE ID=110";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query); // Line 13
System.out.println("Employee ID: " + rs.getInt("ID")); // Line 14
} catch (Exception se) {
System.out.println("Error");
}
Assume that the SQL query matches one record. What is the result of compiling and executing
this code?
A.
The code prints error.
B.
The code prints the employee ID.
C.
Compilation fails due to an error at line 13.
D.
Compilation fails due to an error at line 14.
Explanation:
Assuming that the connection conn has been set up fine, the code will compile and
run fine.
Note#1: The GetInt method retrieves the value of the designated column in the current row of
this ResultSet object as an int in the Java programming language.
Note 2: A table of data representing a database result set, which is usually generated by executing
a statement that queries the database.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is
positioned before the first row. The next method moves the cursor to the next row, and because it
returns false when there are no more rows in the ResultSet object, it can be used in a while loop to
iterate through the result set.
A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you
can iterate through it only once and only from the first row to the last row. It is possible to produce
ResultSet objects that are scrollable and/or updatable.
Reference: The Java Tutorials,Interface ResultSet
A
A indeed. The method does not call next() method of ResultSet.