0

Can I get table name from

ResultSetMetaData
query is join of multiple tables

example

select * from table1 , table 2

when I am going to try to retrieve table name from

ResultSetMetaData
I always founds empty value.

Note : I am using informix driver

2 Answers2

1

Based on the Informix JDBC Guide, the driver is unable to retrieve the tablename if the query accesses more than one table and will return a single space instead:

ResultSetMetaData.getTableName()

Returns the table name for SELECT, INSERT, and UPDATE statements

SELECT statements with more than one table name and all other statements return a String object containing one blank space.

From: Unsupported methods and methods that behave differently

Community
  • 1
  • 1
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

You should use it together with column number parameter, so try something like

String table1 = rs.getMetaData().getTableName(someColumnNumberFromFirstTable);    
String table2 = rs.getMetaData().getTableName(someColumnNumberFromSecondTable);

Also see the docs.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • That will only work if the driver supports this, and as the actual question says it is always empty, I'd assume the driver doesn't support this. – Mark Rotteveel Oct 07 '13 at 12:19
  • Thank you Petr Mensik I am using same as you described. but when getting table name for such queries having joins and same name columns resultset metadata always returns empty table name – Sharjeel Afzal Oct 07 '13 at 12:20
  • 1
    So I guess @MarkRotteveel is right. However I would expect something like `OperationNotSuppurtedException` after this call. – Petr Mensik Oct 07 '13 at 12:24
  • @PetrMensik The [api](http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html#getTableName(int)) doesn't allow that, it only allows throwing an exception if "a database access error occurs" (eg no connection, or an information request failed). As JDBC tries to cover a wide range of vendors which aren't always able to provide that information, it is pretty lenient when it comes to returned metadata (where an empty result in this specific case is considered correct). – Mark Rotteveel Oct 07 '13 at 12:30