DB2的 jdbc驅動升級到V4后使用 ResultSetMetaData.getColumnName不能獲取列的別名的問題
最近把db2的jdbc驅動升級到了 Version4.0, 遇到下面的問題:
以前的代碼中通過ResultSetMetaData.getColumnName來獲取查詢語句列的別名,更新后獲取的是列的名字。
經查是因為IBM根據jdbc的規范進行了相關修改, 官方信息和解決方案如下:
ResultSetMetaData returns different values for the IBM Data Server Driver for JDBC and SQLJ Version 4.0
For the IBM® Data Server Driver for JDBC and SQLJ Version 4.0, the values that are returned for ResultSetMetaData.getColumnName and ResultSetMetaData.getColumnLabel are changed to conform to the JDBC 4.0 standard. Those values differ from the values that are returned for the IBM Data Server Driver for JDBC and SQLJ Version 3.50 and earlier JDBC drivers.
Details
The results vary, depending on the following factors:
- Which data source type and version you are using. DB2® for z/OS® and OS/390 Version 7 and DB2 for i5/OS® V5R2 are unaffected by this change. Only later versions of these database products and all versions of DB2 Database for Linux, UNIX, and Windows and IBM Informix® Dynamic Server are affected.
- Whether a column in the SELECT list of a query has an AS clause. For JDBC drivers before the IBM Data Server Driver for JDBC and SQLJ Version 4.0, if a column in the SELECT list of a query contains an AS clause, ResultSetMetaData.getColumnName returns the argument of the AS clause. Under the IBM Data Server Driver for JDBC and SQLJ Version 4.0, ResultSetMetaData.getColumnName returns the table column name.
- Whether a column in the SELECT list of a query has a label from a LABEL statement but no AS clause. DB2 for z/OS and DB2 for System i support the LABEL statement, which assigns a label to a column. For JDBC drivers before the IBM Data Server Driver for JDBC and SQLJ Version 4.0, if a column in the SELECT list of a query has a label, ResultSetMetaData.getColumnName returns the table column name, and ResultSetMetaData.getColumnLabel returns the column label from the LABEL statement. Under the IBM Data Server Driver for JDBC and SQLJ Version 4.0, ResultSetMetaData.getColumnName and ResultSetMetaData.getColumnLabel return the table column name. The column label from the LABEL statement is not used.
- Whether a column in the SELECT list has a label from a LABEL statement and an AS clause. For JDBC drivers before the IBM Data Server Driver for JDBC and SQLJ Version 4.0, if a column in the SELECT list of a query has a label and an AS clause, ResultSetMetaData.getColumnName returns the argument of the AS clause, and ResultSetMetaData.getColumnLabel returns the column label from the LABEL statement. Under the IBM Data Server Driver for JDBC and SQLJ Version 4.0, ResultSetMetaData.getColumnName returns the table column name, and ResultSetMetaData.getColumnLabel returns the argument of the AS clause. The column label from the LABEL statement is not used.
Resolution
If you cannot change your applications to conform to the new
ResultSetMetaData
behavior but you need other features of JDBC 4.0, set the useJDBC4ColumnNameAndLabelSemantics
Connection
or
DataSource
property to
DB2BaseDataSource.NO
(2) to keep the old behavior.
我使用的是tomcat的datasource, 所以在datasource配置中增加連接屬性設置:connectionProperties =“useJDBC4ColumnNameAndLabelSemantics=2”
參考: