Skip to content

Kode Java - Learn Java by Examples

  • Partners

ResultSetMetaData

How do I get column names of a table using ResultSetMetaData?

Last update: May 31, 2024 by Wayan

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 the ResultSetMetaData class might help you to create an inquiry program where you don’t have all information about a table columns.

package org.kodejava.jdbc;

import java.sql.*;
import java.util.ArrayList;

public class MetadataColumnExample {
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "kodejava";
    private static final String PASSWORD = "s3cr*t";

    public static void main(String[] args) {
        try (Connection 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 book");

            // 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<>();
            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 itself.
            while (resultSet.next()) {
                for (String columnName : columns) {
                    String value = resultSet.getString(columnName);
                    System.out.println(columnName + " = " + value);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Some result generated by our above examples are:

id = 1
isbn = 978-1491910771
title = Head First Java: A Brain-Friendly Guide
published_year = 2022
price = 45.49

Maven Dependencies

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.4.0</version>
</dependency>

Maven Central

Categories JDBC Tags JDBC, ResultSetMetaData Leave a comment
Newer posts
← Previous Page1 Page2

  • Java
    • 2D API
    • Applet
    • AWT
    • Basic
    • Beans
    • Concurrency
    • Core API
    • Cryptography
    • Generics
    • HttpClient
    • IO
    • Java Date Time API
    • JDBC
    • JNDI
    • Lang Package
    • Logging
    • Mail
    • Management
    • Math
    • Net Package
    • Networking
    • Print Service
    • Reflection
    • Regex
    • Scripting
    • Security
    • Sound API
    • Stream API
    • Swing
    • Text Package
    • Util Package
    • XML
    • Zip and GZIP
    • Java 8
    • Java 9
    • Java 10
    • Java 11
    • Java 12
    • Java 14
    • Java 15
    • Java 16
    • Java 17
    • Java 21
    • Java 24
    • Java 25
  • Spring
    • Spring AI
    • Spring Boot
    • Spring Core
    • Spring Data JPA
    • Spring JDBC
    • Spring JMS
    • Spring MVC
    • Spring Security
  • Persistence
    • Hibernate
    • Java Persistence API
    • MyBatis
  • Servlet
    • JSP
    • Servlet
    • Taglib
  • Other Libraries
    • Apache Commons
    • Apache HttpComponents
    • Apache POI
    • E-iceblue Spire
    • Google Gson
    • iText PDF
    • Jackson
    • Jasypt
    • JDOM
    • Joda-Time
    • jPOS
    • JSch
    • JSON-Java
    • Project Lombok
    • ZK Framework
  • Android
    • Android Misc
    • Android Programming
  • Game Programming
    • Java 2D Game Programming
  • Patterns
    • Creational Patterns
  • Swing Loops
    • Animation
  • JVM Languages
    • Kotlin
  • Databases
    • Learning SQL
    • Microsoft Access
    • MongoDB
    • MySQL
  • Tools
    • Docker
    • Eclipse
    • Gradle
    • IntelliJ IDEA
    • JUnit
    • Maven
    • OS X
    • PlantUML
    • Ubuntu
    • UML
  • Partners
    • Buku Java ID

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

© 2026 Kode Java - Learn Java by Examples • Built with ♡ and ☕ in Bali, Indonesia