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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能