简体   繁体   中英

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?

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 :)

Use ResultSetMetaData.getColumnLabel(int)

Gets the designated column's suggested title for use in printouts and displays. The suggested title is usually specified by the SQL AS clause. If a SQL AS is not specified, the value returned from getColumnLabel will be the same as the value returned by the getColumnName method.

Have you tried:

resultset.getMetadata().getColumnLabel(int) 

https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html#getColumnLabel(int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM