Question:
Is there a way to retrieve the name of a column in a java.sql.ResultSet as a string using the column's index?
Answer:
Yes, it is possible to obtain column names using the ResultSetMetaData class.
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2"); ResultSetMetaData rsmd = rs.getMetaData(); String name = rsmd.getColumnName(1);
This code retrieves the name of the first column in the result set.
If you have an SQL query with an alias for a column, such as:
select x as y from table
You can use getColumnLabel() to get the retrieved label name.
String label = rsmd.getColumnLabel(1);
The above is the detailed content of How to Get Column Names from a java.sql.ResultSet by Index?. For more information, please follow other related articles on the PHP Chinese website!