user1610075
user1610075

Reputation: 1641

Get/Set column names after join

I got a query made like this:

String query="select* from Table1 T1 join Table2 T2 on T1.id=T2.id where T1.id=someid";

Now, I execute it like this:

Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(query);

but now I'd like to refer to ResultSet columns like 'T1.colName', as T1 and T2 have columns with the same name...is it possible?

Upvotes: 1

Views: 166

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 108981

You will need to manually assign aliases to columns with the same name, or you will need to access them by column index. The tablename or table alias is not part of the column label When a ResultSet contains multiple columns with the same label, it will return the first column with that label:

When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned.

(from: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html)

Upvotes: 1

Related Questions