WitnessChrist 2008-12-26 12:50
浏览 547
已采纳

用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 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;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败