开发者

Can I get a table name from a join select resultset metadata

开发者 https://www.devze.com 2022-12-29 14:27 出处:网络
Below is my code trying to retrieve table name form Resultset ResultSet rs = stmt.executeQuery(\"select * from product\");

Below is my code trying to retrieve table name form Resultset

ResultSet rs = stmt.executeQuery("select * from product");

ResultSetMetaData meta = rs.getMetaData();
int count = meta.getColumnCount();
for (int i=0; i<count; i++) {
  System.out.println(meta.getTableName(i));
}

But it returns empty, no mention it is a join select resultset. Is there any开发者_运维百科 other approaches to retrieve table name from reusltset metadata?


Getting table names from ResultSetMetaData is something most DBMS's JDBC-drivers (Oracle, DB2,...) don't imlement since there are many situations where the specification doesn't define what should be returned - e.g. in case of views (view-name or base-table-name), presence of aliases (table-alias or real table-name), results of functions that take multiple or no parameters etc.

So I fear there is no way to get what you want for most DBMSs, least of all in a DBMS-independent manner.


When you create you statement, try setting the scroll/concurrency types like so: conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)

Some drivers require these settings in order to return the table name.

See here for details


The column indices passed to getTableName() start at 1. Change your loop to read:

for (int i=1; i<=count; i++) {
  System.out.println(meta.getTableName(i));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号