0
try{

   connection = dataSource.getConnection();
   callableStatement.setInt(2, clientId);
    ....... // some stuff
   resultSet = callableStatement.executeQuery();

}

Now I have a resultSet , but don't know column names? How do I retrive that?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
H.Ostwal
  • 333
  • 1
  • 5
  • 11

5 Answers5

2

try this...

ResultSetMetaData    rsmd    =    resultSet.getMetaData();
int    columnCount    =    rsmd.getColumnCount();
// The column count starts from 1
for    (int    i=1;   i<=columnCount;    i++ ) {
  String name    =    rsmd.getColumnName(i);
  // Do stuff with name
}
Taimour Ali
  • 96
  • 2
  • 5
0

resultSet.getMetadata() returns you a ResultSetMetaData object that has the column names (e.g. resultSet.getMetadata().getColumnName(1) )

Andrew McGuinness
  • 2,092
  • 13
  • 18
  • If you want to know the names of the columns of the result set, then `getColumnLabel` is more correct. `getColumnLabel` returns the alias or the original column name (if there is no alias), while `getColumnName` returns the name of the underlying column (if any). – Mark Rotteveel Mar 22 '17 at 19:14
0

show this:

ResultSetMetaData rsmd = resultSet.getMetaData();
String name = rsmd.getColumnName(1);
Piotr Rogowski
  • 3,642
  • 19
  • 24
0

you can use ResultSetMetaData

 ResultSetMetaData metadata = resultSet.getMetaData();
int columnCount = metadata.getColumnCount();

ArrayList<String> columns = new ArrayList<String>();
for (int i = 1; i < columnCount; i++) {
  String columnName = metadata.getColumnName(i);
  columns.add(columnName);
}
jos
  • 1,082
  • 5
  • 19
  • 45
0

Following code helps to get the column names of the table.

ResultSetMetaData rsmd = resultSet.getMetaData();
int columnsCount = rsmd.getColumnCount();
int i=1;
while (i <= columnsCount){
    String columnName = rsmd.getColumnName(i);
    i++;
}
VNT
  • 934
  • 2
  • 8
  • 19