For standard queries I have not had a problem with this. I simply:
ResultSet rs = stmt.executeQuery("SELECT a, b FROM table_name");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
for (int i = 1; i < numberOfColumns + 1; i++) {
String fieldName = rsMetaData.getColumnName(i);
System.out.print(fieldName + ", ");
}
But the moment I assign a field name to another name, i.e.
ResultSet rs = stmt.executeQuery("SELECT a AS foo, b AS bar FROM table_name");
It throws an error because fields a and b aren’t found.
How should I cater to this? Thanks
There is a difference between column name and column label. The methods in
ResultSetexpect column labels. So you should use the methodResultSetMetaData.getColumnLabelinstead ofResultSetmetaData.getColumnName.