File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question regarding ResultSet.getXXX(String name) behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question regarding ResultSet.getXXX(String name) behaviour" Watch "Question regarding ResultSet.getXXX(String name) behaviour" New topic
Author

Question regarding ResultSet.getXXX(String name) behaviour

Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

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.





OCAJP 7, OCPJP 7
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3206
    
    2
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mala Gupta
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 245
    
    9
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

Author of Manning's OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding ResultSet.getXXX(String name) behaviour
 
Similar Threads
"Has a" vs "is a" in Interfaces or "Getters in Interface"
Extracting Method names based on Fields
Database Starnge behaviour
How to get object from one class to another class?
EL Question