We can retrieve column names from java.sql.ResultSet to retrieve this we have to use ResultSetMetaData interface because this interface provides some methods with the help of which we can retrieve metadata about the columns in a resultset which includes their names,type and some other properties as well.
import java.sql.*;
public class Example {
public static void main(String[] args) throws SQLException {
// Assuming you have a valid ResultSet object called resultSet
ResultSet resultSet = ...;
ResultSetMetaData metaData = resultSet.getMetaData();
int columnIndex = 1; // Replace with your column index
String columnName = metaData.getColumnName(columnIndex);
System.out.println("Column name: " + columnName);
}
}