Ryan Thames
Ryan Thames

Reputation: 3204

Can a ResultSet handle multiple tables?

Say I have a query like this for example:

select b.id, b.desc, c.context_desc, d.age 
from 
index a, events b, contexts c, user d
where 
a.id = b.id and a.context_id = c.context_id and a.user_id = d.user_id
order by b.id

In Java, can a ResultSet handle multiple tables (assuming the column names are all unique)?

int id = resultSet.getInt("id");
String desc = resultSet.getString("desc");
String context_desc = resultSet.getString("context_desc");
int age = resultSet.getInt("age");

Upvotes: 0

Views: 1788

Answers (2)

Paul Richter
Paul Richter

Reputation: 11072

Absolutely.

The result set merely contains the columns specified by the SELECT clause. It makes absolutely no difference how many tables you have in the FROM clause. In your case, your result set will contain columns id, desc, context_desc, and age, as you've indicated.

Ultimately, as long as your SQL is legal, meaning that column names are unambiguous, uniquely named, and visible, the result set will contain exactly that which you specify (no more, no less).

This is the ResultSet documentation, for more information on how it works and what you can do with it.

Upvotes: 2

Philipp Sander
Philipp Sander

Reputation: 10249

In Java, can a ResultSet handle multiple tables (assuming the column names are all unique)?

Yes this is possible!

If you have 2 columns with the same name, you can give them an alias.

Upvotes: 1

Related Questions