• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding ResultSet.getXXX(String name) behaviour

 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

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
returned.


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.



 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3753
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34686
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mala Gupta
Author
Ranch Hand
Posts: 296
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,

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':


With respect,
Mala
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic