• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another array problem

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a problem with my code and its getting me stumped.

see i have a public void checkLotnum(String lotnum) method
that checks if the parameter passed already exists in the database.
it saves the word "found" in a variable named status declared inside the class but outside the method.

Then, I have a public String getLotnum() that returns the variable status if it has been set to "found"

When I do this, it outputs okay:

u.checkEntryUpload(test[0][3]);

but when I do this, all outputs "found" even if it isn't in the database:


please help.
i'm stumped
[ February 20, 2006: Message edited by: shuini gustav ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is status static?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope.

I initialized status to null.
When the record is found in the database, then it gets populated with the word "found" and "null" if the record is not in the database
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these are the values of my test array


this is supposed to output
found null found found

but what outputs is found found found found

but
when i interchange entries 1 and 3, it works and outputs
null found found found

what's wrong?
[ February 20, 2006: Message edited by: shuini gustav ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post your code?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like the problem is in the way the class definition is set up. You have one instance variable called status. Each time you call the check method, you are testing every element in the result set to see if it is in the input String. However, because that same method is going to be called for each String, if the last row in the result set contains the String you're looking for, then status is going to be set to found. I would modify the method to return the value of status.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean? i do have a method that returns the value of status
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet rs = stmt.executeQuery("SELECT * FROM T_UPLOAD where LOTNUM='"+lotnum+"'");
while (rs.next()) {
String acclevel = rs.getString("lotnum");
if(acclevel.equals(lotnum))
status = "found";
else
status = "null";
}

whats is the column lotnum in your query?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i modified the check method to be like this


and i took out the checklotnum method.
but it still doesn't work
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jherald Lacambra:
ResultSet rs = stmt.executeQuery("SELECT * FROM T_UPLOAD where LOTNUM='"+lotnum+"'");
while (rs.next()) {
String acclevel = rs.getString("lotnum");
if(acclevel.equals(lotnum))
status = "found";
else
status = "null";
}

whats is the column lotnum in your query?


huh?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's wrong with this snippet?
it doesn't go into the if statement if there is no record retrieved from the database


[ February 20, 2006: Message edited by: shuini gustav ]
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shuini gustav:


huh?


i mean on your query select * from table, it doesn't state the column lotnum
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the "found" variable ? Is it a member variable ?
Try to declare it locally (inside checkEntryUpload),
and initialize it to "not found".

String found = "not found";

(If I were you, I'd return a boolean, rather than a String)
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jherald Lacambra:


i mean on your query select * from table, it doesn't state the column lotnum


you meant lotnum instead of the asterisk?
it's the same thing, with the asterisk, i'm just retrieving all fields and not just the lotnum
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
Where is the "found" variable ? Is it a member variable ?
Try to declare it locally (inside checkEntryUpload),
and initialize it to "not found".

String found = "not found";

(If I were you, I'd return a boolean, rather than a String)


gee thanks
i did this


and it worked!!!
thanks a lot!!!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Be careful to


"acclevel" will probably never be null,
but you should add a little null check just in case

2. Be careful also to close the connection and clean everything when an Exception occurs. For example, putting it all in a "finally block".
[ February 20, 2006: Message edited by: Satou kurinosuke ]
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help.

i don't know how to implement a finally block though. but i'll try to add it.

what happens if i don't clean after an exception occurs anyway?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what happens if i don't clean

It's dirty

You should clean up things. Especially ResultSets, Statements, Connections, all JDBC related stuff, otherwise you'll find yourself in trouble one day or another.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what exactly do you mean by i'll find myself in trouble one day?
coz i don't usually clean because i don't know how to

and what does cleaning also mean? like implementing the finally clause?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic