File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes another array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another array problem" Watch "another array problem" New topic
Author

another array problem

Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
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

Joined: Feb 07, 2005
Posts: 2367
Is status static?
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
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

Joined: Dec 20, 2005
Posts: 170
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

Joined: Feb 07, 2005
Posts: 2367
Could you post your code?
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Dec 20, 2005
Posts: 170
what do you mean? i do have a method that returns the value of status
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
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?


jherald
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
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

Joined: Dec 20, 2005
Posts: 170
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

Joined: Dec 20, 2005
Posts: 170
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

Joined: Feb 02, 2005
Posts: 129
Originally posted by shuini gustav:


huh?


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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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)


[My Blog]
All roads lead to JavaRanch
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
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

Joined: Dec 20, 2005
Posts: 170
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Dec 20, 2005
Posts: 170
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Dec 20, 2005
Posts: 170
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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: another array problem