wood burning stoves 2.0*
The moose likes JDBC and the fly likes What am I doing wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What am I doing wrong?" Watch "What am I doing wrong?" New topic
Author

What am I doing wrong?

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I'm trying to write a program that updates a table on SQL server. When I hardcode a ref number into my select statement and update it, it seems to work ok. But when I select all of the records from the table and try to update them, it doesn't update a darn thing. I've looked at it for a very long time trying to figure out what stupid mistakes I made, but can't find what I'm doing wrong. I was hoping someone could look at this code and help me out a bit??

Thanks for any help!!!

What I'm trying to do is trying to take pos. 3-4 of the customer code field(user5), check if they're blank(if so it's OK), if it's not blank, I want to move the value of those two positions to the beginning of the fields and fill the rest with blanks.
[ June 28, 2002: Message edited by: Jennifer Sohl ]
[ June 28, 2002: Message edited by: Dirk Schreckmann ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jennifer
The first thing that comes to mind is the fact that you're using the same Statment object to do both of your queries. If you use a Statement object and get a ResultSet back from a query then executing another query on that statement closes the current ResultSet. So at best your update would only work once, because after the ResultSet is closed. Does it update the first one that needs to be updated?
I'll take a look at the rest of it now but I wanted to tell you that first.
I looked at the rest of it and ust wanted to make sure you know that variable last2 will only be getting 1 character when the replace method is run. replace goes from the start postion to the last position -1. So the line:
last2.replace(0,1, rs.getString(1).substring(2,4));
is only going to replace the character at position 0 in the StringBuffer. The same applys to the other times you use replace:
user5all.replace(0,9,rs.getString(1));
refno.replace(0,9, rs.getString(2));
both of those are going to get their first 8 characters replaced, if that's the wrong number of characters your replace statement wont get any matching rows so it wont replace anything.
I would put in some print lines to see if that code is getting executed at all and also, maybe create the SQL outside of the excuteUpdate method and then print it to see exactly what it looks like.
hope that helps
[ June 28, 2002: Message edited by: Dave Vick ]

Dave
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Dave,
Thanks for the help! I created a different statement object for my update and it worked beautifully. Except of course for my StringBuffers that needed a bit of tweaking. Need to read up on those a bit more.
You are my hero.
I think I'll go drink a beer to that!!
Thanks again!!!
 
wood burning stoves
 
subject: What am I doing wrong?
 
Similar Threads
Constructor confusion...
What am I doing wrong?
Statement is closed?
What am I doing wrong?
Result Set problems