JDBC – How to get the column name (not the original column name)

I have a query something like this which rename the column name animal to bird.

select animal as bird 
from table;

I want to retrieve the column name bird through Jdbc and not the column name animal. When I do

Connection cn = DriverManager.getConnection(host,username,password);
Statement st = cn.createStatement();
ResultSet resultset = statement.executeQuery(query);
resultset.getmetadata().getcolumnName(int) 

I get the original column name instead of the renamed column name. How can I get the renamed column name?

Answer

There are two functions:

 ResultSetMetaData.getColumnName()

The name function will give you the actual column name of the CREATE TABLE statement.

and

 ResultSetMetaData.getColumnLabel()

The label function will give you the renamed column name.

So in your case you have to go for the getColumnLabel() function 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *