I want to know if there is a way to get a column name based on the index from a resultSet.
I know that if you want to get index based on the columnName, you can do that by using
int index = resultSet.findColumn(columnName);
But I need the other way around, something like :
String column = resultSet.findColumnName(index);
Is it possible?
I think you need to look at
ResultSet.getMetaData()which returns the meta-data associated with aResultSet.You can then iterate over the columns (use
getColumnCount()to find out how many there are) to find the column with the given name, checking withgetColumnName(). Don’t forget that column indexes are 1-based, rather than 0-based. Something like:If you need to do this for a lot of names, you may want to build a
HashMap<String, Integer>to map them easily.