failing to match strings with ResultSet.getString(int)

B

balgach

Hello all, i am trying to compare two exact strings and seem to be
failing at it, and just wondering what i am doing wrong. basically i
have a postgresql table:

foo(t1, t2); both t1 and t2 are of type char(20).

when i do in postgresql a: select t1 from foo where t2='bla'; it
returns one tuple with the value "test" now here is my java code:

ResultSet results = sql.executeQuery("select t1 from foo where
t2='bla'");
String _t = "test";
if (results != null) {
results.next();
System.out.println(results.getString(1)+"\n");
String _s = new String(results.getString(1));
if (_s==_t)
System.out.println("Matched\n");
else
System.out.println("NO MATCH\n");
}
results.close();
}

it returns the right thing (a string that says "test") in the
System.out line, but the comparison keeps sending me to the else
statement, not the first one. anyone know why?

Cheers,
Adam.
 
F

foo

You have to use _s.equals(_t) instead of the reference comparison.
_s and _t do not refer to the same object.
 
B

Betty

foo said:
You have to use _s.equals(_t) instead of the reference comparison.
_s and _t do not refer to the same object.

Just curious why you are using identifiers that start with underscore?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top