java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get column names of a table using ResultSetMetaData?

This example show how we can use the ResultSetMetadata class to get the number of columns and column names of the selected table. The ResultSetMetaData class can also be used to get the column type and its properties.

Using this class might help you to create an inquiry program where you don't have all information about table columns.

package com.javacoderanch.example.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.ArrayList;

public class MetadataColumnExample {
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost/testdb";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";
    
    public static void main(String[] args) throws Exception {
	Connection connection = null;
	try {
	    //
	    // As the usual ritual, load the driver class and get connection
	    // from database.
	    //
	    Class.forName(DRIVER);	    
	    connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
	    
	    //
	    // In the statement below we'll select all records from users table
	    // and then try to find all the columns it has.
	    //
	    Statement statement = connection.createStatement();
	    ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
	    
	    //
	    // The ResultSetMetaData is where all metadata related information
	    // for a result set is stored.
	    //
	    ResultSetMetaData metadata = resultSet.getMetaData();
	    int columnCount = metadata.getColumnCount();
	    
	    // 
	    // To get the column names we do a loop for a number of column count
	    // returned above. And please remember a JDBC operation is 1-indexed
	    // so every index begin from 1 not 0 as in array.
	    //
	    ArrayList<String> columns = new ArrayList<String>();
	    for (int i = 1; i < columnCount; i++) {
		String columnName = metadata.getColumnName(i);
		columns.add(columnName);
	    }
	    	    
	    //
	    // Later we use the collected column names to get the value of the
	    // column it self.
	    //
	    while (resultSet.next()) {
		for (String columnName : columns) {
		    String value = resultSet.getString(columnName);
		    System.out.println(columnName + " = " + value);
		}
	    }
	    
	} catch (SQLException e) {
	    e.printStackTrace();
	} finally {
	    connection.close();
	}
    }
}

Some result generated by our above examples are:

id = 1
username = admin
password = secret
first_name = john
last_name = doe
created = 2007-12-18 10:52:55.0
create_by = system
modified = 2007-12-18 10:52:55.0