Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

JDBC Question: Getting data from tables with columns that ..

 
   Database Forums (Home) -> Java RSS
Next:  Dont buy Gid php 5  
Author Message
lennyw

External


Since: Jun 29, 2005
Posts: 1



(Msg. 1) Posted: Wed Jun 29, 2005 6:35 pm
Post subject: JDBC Question: Getting data from tables with columns that ha
Archived from groups: comp>lang>java>databases (more info?)

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(Cooll
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

 >> Stay informed about: JDBC Question: Getting data from tables with columns that .. 
Back to top
Login to vote
joe.weinstein

External


Since: Feb 21, 2005
Posts: 87



(Msg. 2) Posted: Wed Jun 29, 2005 8:07 pm
Post subject: Re: JDBC Question: Getting data from tables with columns tha [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

 >> Stay informed about: JDBC Question: Getting data from tables with columns that .. 
Back to top
Login to vote
Thomas Kellerer

External


Since: Jun 30, 2005
Posts: 24



(Msg. 3) Posted: Thu Jun 30, 2005 2:55 am
Post subject: Re: JDBC Question: Getting data from tables with columns tha [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 30.06.2005 03:35 lennyw wrote:
> 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");

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
 >> Stay informed about: JDBC Question: Getting data from tables with columns that .. 
Back to top
Login to vote
lennyw

External


Since: Jun 30, 2005
Posts: 1



(Msg. 4) Posted: Thu Jun 30, 2005 7:54 am
Post subject: Re: JDBC Question: Getting data from tables with columns tha [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thomas Kellerer wrote:
> On 30.06.2005 03:35 lennyw wrote:
> > 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");
>
> 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

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
 >> Stay informed about: JDBC Question: Getting data from tables with columns that .. 
Back to top
Login to vote
Thomas Kellerer

External


Since: Jun 30, 2005
Posts: 24



(Msg. 5) Posted: Thu Jun 30, 2005 11:55 am
Post subject: Re: JDBC Question: Getting data from tables with columns tha [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 30.06.2005 16:54 wrote:
> 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" ?

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 Smile

"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...ml#4844

Thomas
 >> Stay informed about: JDBC Question: Getting data from tables with columns that .. 
Back to top
Login to vote
Lee Fesperman

External


Since: Aug 10, 2003
Posts: 171



(Msg. 6) Posted: Thu Jun 30, 2005 8:55 pm
Post subject: Re: JDBC Question: Getting data from tables with columns tha [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

lennyw wrote:
>
> 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(Cooll
> 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?

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.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
 >> Stay informed about: JDBC Question: Getting data from tables with columns that .. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Accessing linked tables in MS Access database using JDBC O.. - Does anyone know if JDBC can access linked tables in a Microsoft Access? We have a Mictosoft Access database that contains linked tables that are physically on a network drive. On a windows machine, We set up a java servlet that uses JDBC to connect to ...

Accessing linked tables in MS Access database using JDBC O.. - Does anyone know if JDBC can access linked tables in a Microsoft Access? We have a Mictosoft Access database that contains linked tables that are physically on a network drive. On a windows machine, We set up a java servlet that uses JDBC to connect to ...

JDBC oracle question - Hello, I'm building a wireless application with Java J2ME (CDC profile). My application needs a direct connection with a remote Oracle database. For this purpose I think it's best to use JDBC. But which JDBC driver should I use? - Should I use an..

best way to extract the data from several tables into a file - hi, I have to extract the data from several tables (Oracle is the database) and insert them into a file, in a specified format. Each column will be allocated a certain size (filled with empty spaces if size is too big), and I will assign a line for eac...

Testing for empty columns in resultset - This seems too trivial. After populating a resultset with a query, what is the best way to test if a column is null to avoid the dreaded null pointer exception when attempting a tostring() call. thanks
   Database Forums (Home) -> Java All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]