getting column names
I'm trying to write some java code that requires dynamically knowing the names of the columns of a table.
Is there a way to fetch the column names of a table? Either through a query or just be saying "get column names" ? I'd get them and then populate the column names into an array.
Thanks
ps - if it makes a difference, the database I'm programming against is MySQL
> Is there a way to fetch the column names of a table?
> Either through a query or just be saying "get column
> names" ? I'd get them and then populate the column
> names into an array.
Here's a way I did it. Might not be the best way but it works.
private static Object[] createColumnNameArray(ResultSet rs)
throws SQLException{
int col = 0;
col = getColumnCount(rs);
String columnLabel = null;
Object[] columnNames = new Object[col];
ResultSetMetaData rsmd = null;
rsmd = rs.getMetaData();
for (int i = 0; i < col; i++){
columnNames[i] = rsmd.getColumnLabel(i+1);
}
return columnNames;
}
thanks.
mp