views:

271

answers:

2

Hello

I'm having issues with the Java code below. It is supposed to update certain records in a table where the ID is given, and where the STATUS column is 'good' (this is only one row at any given time). However, when I run the code below, it seems to be ignoring the AND STATUS = 'good' part, and updating all NUMRECS wherever the ID matches.

static void insertNumRecs()
    {
        PreparedStatement insert = null;
        try
        {
            String insertNumRecsCommand = "UPDATE FILESTATUS SET NUMRECS = ? " +
                    "WHERE ID = ? AND STATUS = 'good'";
            insert = Main.con.prepareStatement(insertNumRecsCommand);
            insert.setInt(1, Main.numRecs);
            insert.setString(2, Main.docID);
            insert.executeUpdate();
        }
        catch (Exception ex) {ex.printStackTrace();}
        finally {close(null, insert);}
    }

I've tried searching for this everywhere, but I couldn't find any answers. When I run the command directly from the database, it works fine, which confuses me even more.

Thanks in advance.

+2  A: 

Try to write

"WHERE ID = ? AND STATUS = ?"

and use

insert.setString(3, "good");
Peter Lang
Oh my, do I feel dumb. I didn't even think of this. Works perfectly!However, I am somewhat curious as to why what I did above did not work... any ideas?Thank you!
ryantmer
@ryantmer: No ideas, sorry. I tried the something similar right now, and it just worked fine. Only matching rows updated...
Peter Lang
+2  A: 

This doesn't explain the problem, but I'd wonder why you don't do this:

static void insertNumRecs() 
    { 
        PreparedStatement insert = null; 
        try 
        { 
            String insertNumRecsCommand = "UPDATE FILESTATUS SET NUMRECS = ? " + 
                    "WHERE ID = ? AND STATUS = ?"; 
            insert = Main.con.prepareStatement(insertNumRecsCommand); 
            insert.setInt(1, Main.numRecs); 
            insert.setString(2, Main.docID); 
            insert.setString(3, "good");
            insert.executeUpdate(); 
        } 
        catch (Exception ex) {ex.printStackTrace();} 
        finally {close(null, insert);} 
    } 

Can't see your data, so I can't tell if it's a case issue ("GOOD" != "good")

Sure you're connecting to the database you think you are? If the connection string points to one database, and you run your test against another, that would explain why you don't see the change.

duffymo
Thanks! Peter Lang was quicker on the trigger though ;)And the case is correct, I made sure to keep everything lowercase for that reason. As for the connection, everything else works fine, so I have no idea why this wouldn't! =/
ryantmer