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 

目录
打赏
0
0
0
0
95
分享
相关文章
java.sql.ResultSetMetaData.getColumnLabel和getColumnName的区别
如果将ResultSet的结果映射到HashMap中,要使用getColumnLabel,而不要用getColumnName,这样可提高程序的健壮性 理由: getColumnName返回的是sql语句中field的原始名字。
928 0
Java -- JDBC 学习--通过 ResultSet 执行查询操作
ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果.   1. 调用 Statement 对象的 executeQuery(sql) 可以得到结果集。   2. ResultSet 返回的实际上就是一张数据表. 有一个指针指向数据表的第一样的前面.可以调用 next() 方法检测下一行是否有效. 若有效该方法返回 true, 且指针下移. 相当于Iterator 对象的 hasNext() 和 next() 方法的结合体。
1035 0
让java从Mysql返回多个ResultSet
首先,JDBC对于SQLSERVER来说默认是支持返回,但对于MySql来说,只默认支持存储过程返回多个ResultSet,那对于手写SQL怎么办。 其实很简单,只要一个在连接字符串中加一个参数:allowMultiQueries=true。
1292 0
一个坑:java.sql.ResultSet.getInt==》the column value; if the value is SQL NULL, the value returned is 0
Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.
1001 0
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
详细错误信息: org.springframework.dao.TransientDataAccessResourceException: ### Error querying database.
930 0
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/50015665 详细错误信息: org.
1268 0
Java中的return语句深入解析
Java中的return语句深入解析
503 0
java使用jdbc连接ResultSet通过next()取不到第一条数据
java使用jdbc连接ResultSet通过next()取不到第一条数据
362 0
使用QueryRunner报错:java.sql.SQLException: Cannot create
使用QueryRunner报错:java.sql.SQLException: Cannot create
221 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等