WitnessChrist
2008-12-26 12:50 阅读 417
已采纳

用JDBC如何获得字段列别名?

项目中需要获得返回结果集的列别名,但是找不到方法。请大家帮忙。

比如:SELECT column_1 as c1 ,column_2 as c2 from table

connection = DriverManager.getConnection(URL, userName, password);

//这个方法能获得数据库是否支持别名
DatabaseMetaData dbMeta = connection.getMetaData();
System.out.println(dbMeta.supportsColumnAliasing());

preparedstatement = connection.prepareStatement(s1);
ResultSet resultset = preparedstatement.executeQuery();
ResultSetMetaData resultsetmetadata = resultset.getMetaData();

//[color=red]这里只能获取到字段的实际名称 column_1 和 column_2[/color]
//[color=red]但是无法获得别名 c1 和 c2[/color]
resultsetmetadata.getColumnName(1);
resultsetmetadata.getColumnName(2);

请问我想[color=red]要获取别名 c1 和 c2 [/color]应当怎么操作呢?JavaAPI中有对应的方法吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    anndefans anndefans 2008-12-26 13:10

    /**
    * 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.
    *
    * @param column the first column is 1, the second is 2, ...
    * @return the suggested column title
    * @exception SQLException if a database access error occurs
    */
    String getColumnLabel(int column) throws SQLException;

    点赞 评论 复制链接分享
  • anndefans anndefans 2008-12-26 13:11

    ResultSetMetaData.getColumnLabel(int column) throws SQLException;

    点赞 评论 复制链接分享

相关推荐