What is the result?

Given this code fragment:

public static void main(String[] args) {

try {

String query = “SELECT * FROM Item”;

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

ResultSetMetaData rsmd = rs.getMetaData(); // Line 14

int colCount = rsmd.getColumnCount();

while (rs.next()) {

for (int i = 1; i <= colCount; i++) {

System.out.print(rs.getObject(i) + ” “); // Line 17

}

System.out.println();

}

} catch (SQLException se) {

System.out.println(“Error”);

}

Assume that the SQL query returns records.
What is the result?

Given this code fragment:

public static void main(String[] args) {

try {

String query = “SELECT * FROM Item”;

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

ResultSetMetaData rsmd = rs.getMetaData(); // Line 14

int colCount = rsmd.getColumnCount();

while (rs.next()) {

for (int i = 1; i <= colCount; i++) {

System.out.print(rs.getObject(i) + ” “); // Line 17

}

System.out.println();

}

} catch (SQLException se) {

System.out.println(“Error”);

}

Assume that the SQL query returns records.
What is the result?

A.
Compilation fails due to error at line 17

B.
The program prints Error

C.
The program prints each record

D.
Compilation fails at line 14



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Peter

Peter

C

nmm

nmm

D
rs = stmt.executeQuery(“SELECT * FROM EMPLOYEE”);
// Get the ResultSet from the query
rsmtadta = rs.getMetaData(); // Create a ResultSetMetaData object 1
colCount = rsmtadta.getColumnCount(); 2
// Find number of columns in EMP
for (i=1; i<= colCount; i++) { 3
colName = rsmtadta.getColumnName(); // Get column name
colType = rsmtadta.getColumnTypeName();
// Get column data type
System.out.println("Column = " + colName +
" is data type " + colType);
// Print the column value
}