java.sql.ResultSetMetaData.getColumnLabel和getColumnName的区别

简介: 如果将ResultSet的结果映射到HashMap中,要使用getColumnLabel,而不要用getColumnName,这样可提高程序的健壮性 理由: getColumnName返回的是sql语句中field的原始名字。

如果将ResultSet的结果映射到HashMap中,要使用getColumnLabel,而不要用getColumnName,这样可提高程序的健壮性

理由:

getColumnName返回的是sql语句中field的原始名字。
getColumnLabel是field的SQL AS的值(Alias--别名)。
比如:select
          a.name as name,
          a.description as description,
          b.description as relatedDescription
         from a,b where ...
此时,getColumnName(3) == "description";而getColumnLabel(3) == "relatedDescription"。

API:

getColumnLabel

String getColumnLabel(int column)
                      throws SQLException
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.

 

Parameters:
column - the first column is 1, the second is 2, ...
Returns:
the suggested column title
Throws:
SQLException - if a database access error occurs

 

getColumnName

String getColumnName(int column)
                     throws SQLException
Get the designated column's name.

 Parameters:

column - the first column is 1, the second is 2, ...
Returns:
column name
Throws:
SQLException - if a database access error occurs

 

http://blog.sina.com.cn/s/blog_735ef3790100qmf3.html 

相关文章
|
Java 编译器 数据库连接
Cause java.sql.SQLDataException Unsupported conversion from LONG to java.sql.Timestamp
Cause java.sql.SQLDataException Unsupported conversion from LONG to java.sql.Timestamp
794 0
|
Java 关系型数据库 MySQL
【Java异常】java.sql.SQLExcetion:Cannot convert value “0000-00-00 00:00:00” from column 9 to TIMESTAMP
【Java异常】java.sql.SQLExcetion:Cannot convert value “0000-00-00 00:00:00” from column 9 to TIMESTAMP
83 0
|
8月前
|
Java 编译器
Java中的return语句深入解析
Java中的return语句深入解析
452 0
|
druid Java 数据库连接
java报错Error attempting to get column ‘XXX’ from result set. Cause: java.sql.怎么解决?
java报错Error attempting to get column ‘XXX’ from result set. Cause: java.sql.怎么解决?
2585 0
java报错Error attempting to get column ‘XXX’ from result set. Cause: java.sql.怎么解决?
|
SQL 人工智能 Java
SQLException: Value ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp
SQLException: Value ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp
|
Java 关系型数据库 MySQL
java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@27ce24aa is still active
java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@27ce24aa is still active
461 0
|
Java 数据库连接
java使用jdbc连接ResultSet通过next()取不到第一条数据
java使用jdbc连接ResultSet通过next()取不到第一条数据
343 0
Java基础方法深入理解return语句
Java基础方法深入理解return语句
|
JSON Java 数据库
Java数据库ResultSet转json实现
现在有很多json相关的Java工具,如json-lib、gson等,它们可以直接把JavaBean转换成json格式。 在开发中,可能会从数据库中获取数据,希望直接转成json数组,中间不通过bean。
2034 0
使用QueryRunner报错:java.sql.SQLException: Cannot create
使用QueryRunner报错:java.sql.SQLException: Cannot create
205 0