简体   繁体   English

从jdbc的元数据对象获取完整的列名

[英]Getting the full column name from jdbc's metadata object

I'm processing a resultset where the number of returned columns vary and thus I need to know which columns are present. 我正在处理一个结果集,其中返回的列数有所不同,因此我需要知道存在哪些列。 I found that I can extract the returned column names like this: 我发现我可以像这样提取返回的列名:

ResultSetMetaData meta = rs.getMetaData();
ArrayList<String> columns = new ArrayList<String>();
for (int i = 0; i < meta.getColumnCount(); i++) {
    columns.add(meta.getColumnLabel(i+1));
}

This however does not give me the full column name defined in my SQL. 但是,这并没有提供我在SQL中定义的完整列名。 Ie. IE浏览器。

select events.id, events.name from events;

shows up as "id, name" and not "events.id, events.name" which is pretty bad when joining tables and wanting to differ on the column names returned. 显示为“ id,name”而不是“ events.id,events.name”,这在联接表并希望在返回的列名上有所不同时非常糟糕。

You have to specify an alias while joining tables. 连接表时必须指定别名。 There are two methods - getColumnName() - returns a column name and getColumnLabel() - returns a column label (alias). 有两种方法getColumnName() -返回列名和getColumnLabel() -返回列标签(别名)。 If an alias is not specifed then the getColumnLabel() returns same value as the value returned by the getColumnName() . 如果别名不然后specifed的getColumnLabel()返回相同的值由所述返回的值getColumnName()

The answer is: you can't retrieve the table alias from the select statement. 答案是:您无法从select语句中检索表别名。 You can however retrieve the name of the underlying table of a column, so you should be able to retrieve this using getTable(int) from ResultSetMetaData as you are not using table aliases, but are using the actual tables. 但是,您可以检索列的基础表的名称,因此您应该能够使用ResultSetMetaData中的getTable(int)来检索此名称,因为您没有使用表别名,而是使用了实际的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM