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

用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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题