Home arrow static arrow Java Programming [Archive] - Strange problem filling a list with a resultset... anybody can help me?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Strange problem filling a list with a resultset... anybody can help me?
This topic has 3 replies on 1 page.

Posts:1
Registered: 7/2/04
Strange problem filling a list with a resultset... anybody can help me?  
Jul 2, 2004 11:00 AM



 
Hi, first of all excuse my poor english... Here's the problem:

I'm trying ot update a database of products with the products that i read form a file.
So, I read all the references of the database and i fill a list. When i read a the reference of a product from the file i check if exists in the list , if exists i update the product in the database if not exists i insert the product.

And the problem is that sometimes it can't find the prodcut in the list but it exists in the database, so or the statement.executeQuery ( ) is not returning all the data or it don't fill the list correctly or it's a bug...

Here's the code:

java.util.List refExistentes = new Vector(5100);
java.util.List refCodebarP = new Vector (5100);

String selAllProd = "Select cref from articulo;";

ResultSet refRS = stmt.executeQuery(selAllProd);

while (refRS.next()) {

refExistentes.add( refRS.getString("cref").trim());

}

// Code to read the file
// i read the first line and i take de ref of the product
// String ref = reference of the product that is the same thats is stored in the column cref in the database.
//

if ( ! refExistentes.contains(ref) ) {

numAct = stmt.executeUpdate(queryInsertArticulo);

if (numAct == 0) System.err.println("El art�culo " + ref + "no se ha isertado.");


}
else stmt.executeUpdate(queryUpdateArticulo);


Some times doesn't find a product in the list and inserts it again.

If instead of cheking the list, i make a query looking for a product with that reference in the database and inserting or updating depending on that, it works Ok!!! but it's slower...

Any idea?? please help!!

Any advise to improve enhacement will be welcomed!

 

Posts:6,750
Registered: 1/25/04
Re: Strange problem filling a list with a resultset... anybody can help me?  
Jul 2, 2004 11:12 AM (reply 1 of 3)



 
so or the statement.executeQuery ( ) is not
returning all the data or it don't fill the list
correctly or it's a bug...

You need to find out which it is. As you loop through the ResultSet, output each item to a log file or the console (System.out.println). If that's correct, then output the contents of list after you get done. If that's correct, then I would guess you have a problem with your equals(Object) method that's preventing contains from working the way you want.
 

Posts:14
Registered: 1/18/04
Re: Strange problem filling a list with a resultset... anybody can help me?  
Jul 3, 2004 7:17 AM (reply 2 of 3)



 
I agree that it most likely is in the contains (witch uses the compareTo on String). Try to use this:

if ( ! refExistentes.contains(ref.trim()) ) instead of your old code if ( ! refExistentes.contains(ref) )

Perhaps some items in the list have spaces before and/or after the name itself. That would make the compareTo to return values != 0. That is:
"String".compareTo("String ") ==> -1;
whereas
"String".compareTo("String") ==> 0;

But we do not know if you have allready trimmed the ref, as the code is not posted.

If this falis, try using this:

//when inserting in List
refExistentes.add( refRS.getString("cref").trim().toLowerCase() );

//when testing
if ( ! refExistentes.contains(ref.trim().toLowerCase() ) )

Hope it works
Poul Krogh

 

Posts:27,518
Registered: 11/3/97
Re: Strange problem filling a list with a resultset... anybody can help me?  
Jul 3, 2004 2:52 PM (reply 3 of 3)



 
And of course if the table schema uses 'char' rather than 'varchar' then the data will also be blank filled and that will not match to something that is not blank filled.
 
This topic has 3 replies on 1 page.