WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java JDBC · Intermediate · question 36 of 100

What is the role of the ResultSetMetaData interface in JDBC?

📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

The ResultSetMetaData interface in JDBC is used to retrieve information about the metadata (structure and properties) of a Resultset object’s columns. It enables the application to obtain information such as the number of columns in the result set, the names of the columns, their data types, precision, scale, and so on.

The ResultSetMetaData interface can be obtained from a ResultSet object by calling the getMetaData() method. Once you have obtained an instance of ResultSetMetaData, you can use its various getter methods to retrieve metadata information about each column of the ResultSet object.

Here’s an example that demonstrates the use of the ResultSetMetaData interface:

    
    // Create a Statement object to execute the SQL query
    Statement stmt = conn.createStatement();
    
    // Execute the SQL query and obtain a resultset
    ResultSet rs = stmt.executeQuery("SELECT * FROM customers");
    
    // Obtain metadata information for the ResultSet
    ResultSetMetaData rsmd = rs.getMetaData();
    
    // Retrieve metadata about the resultset columns
    int columnCount = rsmd.getColumnCount();
    System.out.println("Number of columns in the result set: " + columnCount);
    
    for (int i = 1; i <= columnCount; i++) {
        String columnName = rsmd.getColumnName(i);
        String columnType = rsmd.getColumnTypeName(i);
        int columnSize = rsmd.getColumnDisplaySize(i);
        System.out.println("Column " + i + " Name: " + columnName + ", Type: " + columnType + ", Size: " + columnSize);
    }

In this example, we first create a Statement object to execute an SQL query. We then execute the query and obtain a ResultSet object. Next, we obtain an instance of ResultSetMetaData from the ResultSet object using the getMetaData() method. Finally, we use the various getter methods of the ResultSetMetaData interface to retrieve metadata information about each column of the ResultSet object. The output of this program would display the number of columns in the result set and metadata information about each column, including its name, type, and size.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java JDBC interview — then scores it.
📞 Practice Java JDBC — free 15 min
📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

All 100 Java JDBC questions · All topics