Is it possible to get the second or the third column name with the same name?
Directly from this API of ResultSet :
Column names used as input to getter methods are case insensitive.
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
What if I have a set of columns with the same name and I would like to reach the third one, for example, with `getXXX(name)`. Is it possible or I would be forced to use `getXXX(int colnum)`?
Thanks in advance.
I haven't seen a case where a resultset has multiple columns with the same name. The query wouldn't execute on the database. If you join multiple tables with select *, the database appends something to the columns names to make the resulting column names unique (usually _1, _2 etc.).
But from the API description, it is clear that you can't get other columns that have the same name. You can get them by index though.
Nick Widelec wrote:What if I have a set of columns with the same name and I would like to reach the third one, for example, with `getXXX(name)`. Is it possible or I would be forced to use `getXXX(int colnum)`?
In addition to Paul's suggestion, you could also change the SQL to avoid that scenario. For example
The simplest way to include multiple columns in a ResultSet is to select the same column from a table more than once. The ResultSet in the following example code includes two columns with the same name-title:
Because the column names used as input to getter methods are case insensitive, all of the following would return the same value:
You can't select multiple columns with the same name from separate tables without using table alias or prefix in a SQL statement. Assuming that you have the following tables:
The following SQL query will return to you a ResultSet with 'title' from table 'book' and 'title' from table 'person'.
Because there are two columns with the same name-'title', getString("title") would return to you 'title' from table 'book' (first occurrence of matching column). If you change positions of person.title and book.title, getString("title") would return to you 'title' from table 'person':