ITEM Table
* ID, INTEGER: PK
* DESCRIP, VARCHAR(100)
* PRICE, REAL
* QUALITY, INTEGER
And given the code fragment (assuming that the SQL query is valid):
try {
String query = “SELECT * FROM Item WHERE ID=110”;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next ()) {
System.out.println(“ID: ” + rs.getInt(“Id”));
System.out.println(“Description: ” + rs.getString(“Descrip”));
System.out.println(“Price: ” + rs.getDouble(“Price”));
System.out.println(“Quantity: ” + rs.getInt(“Quantity”));
}
} catch (SQLException se) {
System.out.println(“Error”);
}
What is the result of compiling and executing this code?
A.
An exception is thrown at runtime
B.
Compile fails
C.
The code prints Error
D.
The code prints information about Item 110
Explanation:
The connection conn is not defined. The code will not compile.
The explanation is ok but the correct answer is C,not B
Yes, it should be C.
Why the correct answer is C, not B ?
The code looks good, not considering the initialization of conn and the close of the resources.
+1 (The code looks good, not considering the initialization of conn and the close of the resources.)
With out connect initialization answer “B”.
assume conn intialization => D true;
Tricky question, the field on the database definition is quaLity, on the code the field is quaNtity, I think it generates an error on runtime based on that fact
If conn is not innitialization: Compile fails
Assume yes: Rum time error because of this line: System.out.println(“Quantity: ” + rs.getInt(“Quantity”)); Quantity QUALITY
but “assuming that the SQL query is valid” => conn is ok. Correct answer is A
not a good question~