Java ResultSetMetaData getColumnLabel() method with example



In this article, we will learn the ResultSetMetaData getColumnLabel() method in Java. When working with databases in Java, the ResultSetMetaData interface provides valuable information about the structure of a ResultSet, such as column names, types, and properties. 

What is getColumnLabel()?

The getColumnLabel() method of the ResultSetMetaData (interface) retrieves the display name of a particular column. This method accepts an integer value representing the index of the column in the current ResultSet object, as an argument.

Syntax ?

String columnLabel = resultSetMetaData.getColumnLabel();
  • Parameters: The index of the column (starting from 1).
  • Returns: A String representing the column's label or name.
  • Throws SQLException: If a database access error occurs or the column index is invalid.

Using getColumnLabel()

The following program demonstrates how to use the getColumnLabel() method. It connects to a MySQL database, retrieves the metadata of a table, and retrieves the suggested title or label for a column in the ResultSet.

To get the ResultSetMetaData object, you need to ?

Register the Driver: Select the required database and register the Driver class of the particular database using the registerDriver() method of the DriverManager class or, the forName() method of the class named Class ?

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Get connection: Create a connection object by passing the URL of the database, username and password of a user in the database (in string format) as parameters to the getConnection() method of the DriverManager class ?

Connection mysqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");

Create a Statement object: Create a Statement object using the createStatement method of the connection interface ?

Statement stmt = con.createStatement();

Execute the Query: Execute the SELECT query using the executeQuery() methods of the Statement interface and Retrieve the results into the ResultSet object ?

String query = "Select * from MyPlayers";
ResultSet rs = stmt.executeQuery(query);

Get the ResultSetMetsdata object: Retrieve the ResultSetMetsdata object of the current ResultSet by invoking the getMetaData() method ?

ResultSetMetaData resultSetMetaData = rs.getMetaData();

Finally, using the getColumnLabel() method of the ResultSetMetaData interface get the column label of the required column by specifying its index as ?

String columnLabel = resultSetMetaData.getColumnLabel(4);

Let us create a table with the name MyPlayers in the MySQL database using the CREATE statement as shown below ?

CREATE TABLE MyPlayers(
   ID INT,
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID)
);

Now, we will insert 7 records in the MyPlayers table using INSERT statements ?

insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India');
insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');

Example

Following JDBC program establishes a connection with MySQL database, retrieves and displays the Column label of a column with index 4 in the MyPlayers table using the getColumnLabel() method ?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSetMetaData_getColumnLabel {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to retrieve records
      String query = "Select ID, First_Name, Last_Name, Date_Of_Birth as DOB,
         Place_Of_Birth as POB, Country from MyPlayers";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      //retrieving the ResultSetMetaData object
      ResultSetMetaData resultSetMetaData = rs.getMetaData();
      //Retrieving the column label
      String columnLabel1 = resultSetMetaData.getColumnLabel(4);
      System.out.println("Column label of the column Date_Of_Birth (4th column): "+ columnLabel1);
   }
}

Output

Connection established......
Column label of the column Date_Of_Birth (4th column): DOB

Conclusion

The getColumnLabel() method in Java's ResultSetMetaData is a powerful tool for retrieving column labels or aliases from a ResultSet. It is especially useful when working with SQL queries that include column aliases or when you need to display column names in a user-friendly format.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-20T18:25:33+05:30

688 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements