0
Follow
0
View

ResultSetMetaData obtains the TableName and ColumnName

NIUNIU030242 注册会员
2023-02-27 18:36

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > for you, You can obtain the column alias using the getColumnLabel(int column) method of ResultSetMetaData instead of the getColumnName(int column) method.

Because the getColumnName(int column) method returns only the column name from the original SQL statement, while the getColumnLabel(int column) method returns the alias or, if there is no alias, the original column name. Therefore, for columns with aliases, you should use the getColumnLabel(int column) method to get the correct column name.

Here is sample code for getting the column name using the getColumnLabel(int column) method:

ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
    String columnName = metaData.getColumnName(i);
    String columnLabel = metaData.getColumnLabel(i);
    String tableName = metaData.getTableName(i);
    System.out.println("Column Name: " + columnName);
    System.out.println("Column Label: " + columnLabel);
    System.out.println("Table Name: " + tableName);
}


In your example, the alias for the first column is "myname," so you can get the correct column name using the getColumnLabel(int column) method.

About the Author

Question Info

Publish Time
2023-02-27 18:36
Update Time
2023-02-27 18:36