Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i get the table name of colums in resultset?

Status
Not open for further replies.

skycom

Programmer
Mar 30, 2001
8
0
0
HK
Hi All
I have this problem when i make a query to database and get a resultset back, i tried to use ResultSetMetaData of that resultset to get the table name of all columns by calling the method getTableName(int), but the method returns a empty String.
How can i solve this problem, or i can get the tables name in another way?
Thanks
 
You can you DatabaseMetaData to get the table name(s) (see example below)... Hope this helps.

Code:
      Class.forName("lotus.jdbc.domino.DominoDriver");
                  Connection con = DriverManager.getConnection("jdbc:domino:/aTestEmailAccount.nsf", "", "");
                  DatabaseMetaData md = con.getMetaData();

                  if (md==null)
                  {
				    System.out.println("No Database Meta Data");
				  }
				  else
				  {
					  ResultSet rs = md.getTables (null, null, null, null);
					  while(rs.next())
					  {
						  System.out.println(rs.getString("TABLE_NAME"));
					  }
					  System.out.println ("******** END OF getTable ********");
					  ResultSet r = md.getColumns(null, null, "__VIM98_", null);
					  while (r.next())
					  {
						  System.out.println(r.getString("COLUMN_NAME"));
					  }
					  System.out.println("******** END OF getColumns ********");
 
thx ur solution, but my situation is that i want to get the table name from a query result.
for exmaple
i run sql "select invoiceid, customerid from invoice, customer where invoice.invoiceid = customer.invoiceid", and get a resultset, how can i know these two columns belong to which table?

is thete no method to get the table name from a query result?
 
Has anyone found an answer for this? I have a similar issue using the following tables:

Create Table BOOK( BookId int, Name varchar(50), AuthorId int )

Create Table AUTHOR( AuthorId int, Name varchar(30) )

and my select is

Select BOOK.*, AUTHOR.*
From BOOK Inner Join AUTHOR On BOOK.AUTHORID = AUTHOR.AUTHORID

As you can see, the ResultSet from this select will have 2 identically named columns called Name. I know I could use an Alias to uniquely ID each column, but I don't really want to do that - I'd rather dynamically find the table associated with each column returned by the select statement.

Any help would be greately appreciated...

Regards...Marc
Independent Software Consultant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top