Consider the following code segment.
Assume that the connection object is valid and statement.
executeQuery()
method successfully returns a ResultSet object with a few rows in it.
Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM contact")){ System.out.println("ID \tfName \tlName \temail \t\tphoneNo"); // from resultSet metadata, find out how many columns are there and then read the column entries// w w w. j a v a 2 s. c o m int numOfColumns = resultSet.getMetaData().getColumnCount(); while (resultSet.next()) { // traverse the columns by index values for(int i = 0; i < numOfColumns; i++) { // since we do not know the data type of the column, we use getObject() System.out.print(resultSet.getObject(i) + "\t"); } System.out.println(""); }
Which of the following statements is true regarding this code segment?
A. The code segment will successfully print the contents of the rows in the ResultSet object. B. The looping header is wrong. To traverse all the columns, it should be for(int i = 0; i <= numOfColumns; i++) { C. The looping header is wrong. To traverse all the columns, it should be for(int i = 1; i <= numOfColumns; i++) { D. The looping header is wrong. To traverse all the columns, it should be for(int i = 1; i < numOfColumns; i++) {
C.
Given N columns in a table, the valid column indexes are from 1 to N and not 0 to N 1.