 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Raquel Guest
|
Posted: Wed Jun 30, 2004 3:29 am Post subject: Simple Display problem .... |
|
|
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.
|
|
| Back to top |
|
 |
V S Rawat Guest
|
Posted: Wed Jun 30, 2004 5:43 am Post subject: Re: Simple Display problem .... |
|
|
Raquel wrote:
| Quote: | 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.
|
Hi Raquel,
I don't know sql, so I tried to convert your snippet using
normal code.
It is working well with String type, and duly printing "YES"
when the said no. is found.
I am not able to understand how you assign that to char[]
Given below is the code and the line
if (cPhoneNo == "2890") {
is giving compilation error.
I have commented that part. Run it and see what gets printed
in the line
System.out.println("char: " + cPhoneNo);
this will give you some idea what you are comparing to what.
Could you post it back after converting to how you are doing
it in your program?
-Rawat
class TestingChar {
public static void main(String[] args) {
String sPhoneNo[] = { "6423", "4510", "3782",
"2890", "1682", "2986", "4501", "0942", "0672" };
char[] cPhoneNo = new char[4];
for (int i = 0; i < sPhoneNo.length; i++) {
System.out.println("String: " + sPhoneNo[i]);
if (sPhoneNo[i] == "2890") {
System.out.println("YES Strring!!");
}
for (int j = 0; j < 4; j++) {
cPhoneNo[j] = sPhoneNo[i].charAt(j);
}
System.out.println("char: " + cPhoneNo);
// if (cPhoneNo == "2890") {
// System.out.println("YES char!!");
// }
}
} // main
} // class
|
|
| Back to top |
|
 |
Nigel Wade Guest
|
Posted: Wed Jun 30, 2004 9:26 am Post subject: Re: Simple Display problem .... |
|
|
On Tue, 29 Jun 2004 23:29:35 -0400, Raquel wrote:
| Quote: | 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:
| Quote: | 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 : [email]nmw (AT) ion (DOT) le.ac.uk[/email]
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
|
|
| Back to top |
|
 |
Raquel Guest
|
Posted: Wed Jun 30, 2004 10:22 am Post subject: Re: Simple Display problem .... |
|
|
Nigel. THIS SOLVED IT...gosh such a simple thing to miss and I have been
cracking my head since hours now..Thank you so much.
Rawat, thank you too for spending time on my problem and replying.
Regards,
Raquel.
|
|
| Back to top |
|
 |
V S Rawat Guest
|
Posted: Wed Jun 30, 2004 1:41 pm Post subject: Re: Simple Display problem .... |
|
|
Nigel Wade wrote:
| Quote: | 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.
|
a related query.
equals() is used for string match, but, in the
above snippet, phoneno is a char[4], thus == should be good
enough for it.
I wrote in my last post that I could not understand
comparision of char[4] with string.
how do I assign "2890" to a char[4]?
---
Rawat
|
|
| Back to top |
|
 |
Raquel Guest
|
Posted: Thu Jul 01, 2004 4:19 am Post subject: Re: Simple Display problem .... |
|
|
Hello Rawat, actually in a RDBMS "Table" when a column (of the table) is
defined as CHAR(4), it essentially means that it can hold any 'String'
from 0 to 4 characters long. I think the confusion is basically because of
semantics difference.
HTH
Raquel.
|
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Sat Jul 03, 2004 8:51 pm Post subject: Re: Simple Display problem .... |
|
|
"Raquel" <raquel (AT) nospam (DOT) com> writes:
| Quote: | Hello Rawat, actually in a RDBMS "Table" when a column (of the table) is
defined as CHAR(4), it essentially means that it can hold any 'String'
from 0 to 4 characters long. I think the confusion is basically because of
semantics difference.
|
Just to be pedantic a CHAR(4) is always either NULL or holds exactly 4
characters. Variable length CHAR is called VARCHAR.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|