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



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

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



Relevant Pages

  • Re: why excute query through JDBC much slow than query analyzer?
    ... why excute query through JDBC much slow than query analyzer? ... The SendStringParametersAsUnicode is a connection string attribute that is ... If you capture the Execution Plan in a SQL Profiler trace while executing ...
    (microsoft.public.sqlserver.jdbcdriver)
  • JDBC Question: Getting data from tables with columns that have the same name
    ... I'm making a JDBC SQL query that looks something like this: ... I get reasonable values in my JDBC result set. ... String st1 = new String; ...
    (comp.lang.java.databases)
  • Re: Setting Query Timeout through JDBC (CA Driver problem?)
    ... I suggest to you not using commands like ""set lockmode ..." ... inside jdbc because they are not intended for being used like that nor ... About query timeout we have not used it, but I do not remember any ...
    (comp.databases.ingres)
  • Re: database toolbox problem
    ... I have found a workaround for this problem. ... This bug is already listed in the Matlab bug reports and ... query = 'ALTER TABLE test ADD test_col VARCHARNOT NULL'; ... "The JDBC spec states that you must either use ...
    (comp.soft-sys.matlab)
  • Re: JDBC / ODBC problem
    ... surprised if you find bugs in databases, JDBC or ODBC drivers. ... Do the query differently. ...
    (comp.lang.java.programmer)