Re: Simple Display problem ....

From: Nigel Wade (nmw_at_ion.le.ac.uk)
Date: 06/30/04


Date: Wed, 30 Jun 2004 10:26:17 +0100

On Tue, 29 Jun 2004 23:29:35 -0400, Raquel wrote:

> I think this really is not a database problem but something wrong with the
> coe.
>
> Following is a very simple piece of SQLJ:
>
> try
> {
> #sql [sqlj4context] sqlj4_iterator = {SELECT PHONENO FROM
> DB2ADM.EMPLOYEE WHERE WORKDEPT = 'D11'};
>
> while(sqlj4_iterator.next())
> {
> System.out.println(sqlj4_iterator.phoneno());
> if (sqlj4_iterator.phoneno() == "2890")
> {
> System.out.println("YES!!");
> }
> }
> sqlj4_iterator.close();
> #sql [sqlj4context] {COMMIT};
> sqlj4context.close();
> } catch (SQLException sqlexcp)
>
>
>
> The output generated, as expected, consists of all the phone nos. (please
> note that I am SELECTing PHONENOs in the query):
>
> 6423
> 4510
> 3782
> 2890
> 1682
> 2986
> 4501
> 0942
> 0672
>
> So far, so good..but why is "YES" not produced in the output when phone
> no. 2890 appears in the output? PHONENO is defined as CHAR(4) in the
> table.
>
> TIA
> Raquel.

Try changing:

> if (sqlj4_iterator.phoneno() == "2890")

to

    if (sqlj4_iterator.phoneno().equals("2890"))

The == operator tests for object reference identity, i.e. do the
references refer to the same object, it doesn't test if the contents of
the referenced objects are the same. That's what the .equals() method is
for.

-- 
Nigel Wade, System Administrator, Space Plasma Physics Group,
            University of Leicester, Leicester, LE1 7RH, UK 
E-mail :    nmw@ion.le.ac.uk 
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555