JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

Using ResultSetMetaData Objects to List All Fields

This section describes how to call the res.getMetaData method to obtain the ResultSetMetaData object, which can be used to list all fields in the result set.

The ResultSet object returned by a SELECT statement also contains column (field) names, lengths and types. You can use res.getMetaData() to retrieve them into a ResultSetMetaData object, which offers the following methods to allow to get different information:

  • getColumnCount() - Returns the number of columns in this ResultSet object.
  • getColumnName(int column) - Get the designated column's name.
  • getColumnTypeName(int column) - Retrieves the designated column's database-specific type name.
  • getPrecision(int column) - Get the designated column's specified column size.

The tutorial program below shows a good example of how to use ResultSetMetaData:

/**
 * ListResultFields.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class ListResultFields {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Load Microsoft JDBC Driver 1.0
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");

// Obtaining a connection to SQL Server
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang;"
        + "database=AdventureWorksLT");

// Getting result meta data
      Statement sta = con.createStatement(); 
      ResultSet res = sta.executeQuery(
        "SELECT * FROM  sys.objects"
        + " WHERE type_desc='USER_TABLE'");
 
      ResultSetMetaData rmd = res.getMetaData();
      System.out.println("Result set columns:");
      for (int i=1; i<=rmd.getColumnCount(); i++) {
        String name = rmd.getColumnName(i);
        String type = rmd.getColumnTypeName(i);
        int size = rmd.getPrecision(i);
        System.out.println("   "+name+", "+type+", "+size);
      }

      con.close();        
    } catch (java.lang.ClassNotFoundException e) {
      System.err.println("ClassNotFoundException: "
        +e.getMessage());
    } catch (SQLException e) {
      System.err.println("SQLException: "
        +e.getMessage());
    }
  }
}

Run this Java program, you will see a list columns (fields) of the system view, sys.objects:

Result set columns:
   name, nvarchar, 128
   object_id, int, 10
   principal_id, int, 10
   schema_id, int, 10
   parent_object_id, int, 10
   type, char, 2
   type_desc, nvarchar, 60
   create_date, datetime, 23
   modify_date, datetime, 23
   is_ms_shipped, bit, 1
   is_published, bit, 1
   is_schema_published, bit, 1

Sections in This Chapter

Commonly Used JDBC Class Methods

Calling createStatement() and executeQuery

Receiving ResultSet Objects from executeQuery

Closing ResultSet Objects - res.close()

Looping through ResultSet with res.next()

Retrieving Field Values using res.get*() Methods

Using ResultSetMetaData Objects to List All Fields

Dr. Herong Yang, updated in 2007
Using ResultSetMetaData Objects to List All Fields