1. 程式人生 > >mysql結果集ResultSet列別名

mysql結果集ResultSet列別名

今天碰到一個mysql結果集別名顯示異常的問題

ResultSetMetaData.getColumnName 和 ResultSetMetaData.getColumnLabel的區別

簡單的講就是
對於查詢語句select cd.id client_data_id from dual來說

ResultSetMetaData.getColumnName(i) 顯示列名`id`
ResultSetMetaData.getColumnLabel(i) 顯示列別名`client_data_id`

程式碼片段如下:

查詢語句:

" select " +
"
dp.product_sk product_sk, " + " dl.location_sk location_sk, " + " pm.id model_id, " + " dd.date_sk date_sk, " + " cd.device_identifier device_identifier, " + " cd.id client_data_id, " + " hour(cd.date) hour_sk, " + " cd.user_identifier user_identifier " + " from " + " balabala ";

結果集轉換過程:

while(rs.next()){
    ArrayList<String> tmp = new ArrayList<>();
    ResultSetMetaData metaData = rs.getMetaData();
    for(int i = 1; i <= metaData.getColumnCount(); i++){
        //tmp.add(metaData.getColumnName(i) + ":" +rs.getString(i));
        tmp.add(metaData.getColumnLabel
(i) + ":" +rs.getString(i)); } Collections.sort(tmp); list.add(tmp.toString()); }