Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2005

Tip: Looking for answers? Try searching our database.

JDBC Question: Getting data from tables with columns that have the same name

Thread view: 
lennyw - 30 Jun 2005 02:35 GMT
Hi

I'm making a JDBC SQL query that looks something like this:

select <a bunch of columns>, a.shortcode, <some more coumns>,
z.shortcode from Table1 a, Table2 b, Table3 z where <join conditions>,
<more conditions>

I get reasonable values in my JDBC result set.

I've been trying to extract the two "shortcode" column data items that
came from Table1 and Table3 from my result set rows with calls like:

String st1 = new String();
String st2 = new String();
st1 = rs.getString("a.shortcode");
st2 = rs.getString("z.shortcode");

or with

st1 = rs.getString("Table1.shortcode");
st2 = rs.getString("Table3.shortcode");

All of these formulations result in a SQL Exception of field name not
recognized.

Though I haven't tried it, I'm sure I could use indexes to get at the
columns:

st1 = rs.getString(8)l
st2 = rs.getString(21);

but this would make maintenance a bit rough down the line, if/when the
query changes.

Am I doing something wrong, or does JDBC not know about table names or
table aliases in getNNN(String s) calls on result sets?

For your info, the database is Oracle 9i, the JDBC driver is in
ojdbc14.jar, which was on the computer when I inherited it, and I
presume that's what is connecting me to the database. I'm writing this
from home so I don't have access to the source code, but part of the
connection string inincludes "OCI8" in it followed by ID password and
service (don't remember the order though).

Any advice would be greatly appreciated!

-Lenny Wintfeld
joeNOSPAM@BEA.com - 30 Jun 2005 04:07 GMT
Hi. Most JDBC drivers aren't told by the DBMS what table name
is associated with a data column, so the JDBC driver won't be
able to distinguish. A column may come from 0, 1, or more
tables.
  The only two options you have are to get them by index,
or altering the SQL to assign unique aliases to each column.

Joe Weinstein at BEA Systems
Thomas Kellerer - 30 Jun 2005 07:18 GMT
> I'm making a JDBC SQL query that looks something like this:
>
[quoted text clipped - 11 lines]
> st1 = rs.getString("a.shortcode");
> st2 = rs.getString("z.shortcode");

Simply give them a column alias:

SELECT <bunch>, a.shortcode as a_shortcode, z.shortcode as z_shortcode
FROM ....

st1 = rs.getString("a_shortcode"),
st2 = rs.getString("z_shortcode");

> String st1 = new String();
> String st2 = new String();

Why do you do that? You create a new object just to throw it away in the next
line (when you assign a new value to the variable).
This is not needed.

Thomas
lennyw@gmail.com - 30 Jun 2005 15:54 GMT
> > I'm making a JDBC SQL query that looks something like this:
> >
[quoted text clipped - 28 lines]
>
> Thomas

Thanks for the advice. I'm a datacomm / realtime / C/C++/assembly
programmer and just learning  SQL, Java, eclipse and SWT
all(unfortuately) at the same time. To me "String st1 = new String()"
declares a reference variable, allocates a String object on the heap
and assigns the reference to that object. And st1 =
rs.getString("a_shortcode") makes use of the String object at the
reference. In java do those two lines actually allocate, delete and
reallocate the String "st1" ?

As for the SQL column alias, I never used one before. Thanks for the
info on it. I'll get additional details in a SQL book.

I appreciate your time in responding to what I'm sure (to you) are
pretty obvious issues.

Regards,
Lenny Wintfeld
Thomas Kellerer - 30 Jun 2005 16:06 GMT
> Thanks for the advice. I'm a datacomm / realtime / C/C++/assembly
> programmer and just learning  SQL, Java, eclipse and SWT
[quoted text clipped - 4 lines]
> reference. In java do those two lines actually allocate, delete and
> reallocate the String "st1" ?

st1 is *not* a String. It is a reference to String object.

String st1 = new String();
st1 = rs.getString("a");

will create two string objects. The first one beeing immediately disposed again
when the st1 reference is pointing to the reference returned by the getString()
method (which eventually did a new String() somewhere)

Take the time to read up on the difference between objects and object
references. This will help you a lot (especially when you discover the first
time that st1 == st2 does not work even :)

"Thinking in Java" is a good starting point:
http://jamesthornton.com/eckel/TIJ-3rd-edition4.0/TIJ3.htm

Reading the language specification won't harm either
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#48440

Thomas
Lee Fesperman - 01 Jul 2005 00:34 GMT
> Hi
>
[quoted text clipped - 33 lines]
> Am I doing something wrong, or does JDBC not know about table names or
> table aliases in getNNN(String s) calls on result sets?

I think the JDBC spec implies that the drivers should only match on column name.

If you can't change the query to use distinct names, the only other choice is to search
the meta data (ResultSetMetaData) yourself. That's not as bad as it sounds.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.