• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating a specific entry

 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
i have a books table and in it the fields id name ammount
i need to update a certain row (like id=3) and reduce the ammount

i think i need to do a preparedstatment but can't figure out how to use it , any ideas ?

thanks to all helpers!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33723
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Yes, you need to use a PreparedStatement. You also need a SQL query that does the update. What do you have so far?
 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i did this , but don't know how to continue the line , i saw something with question marks in it in an example i saw online but don't think it suits my thing


can you break it down for me ? how does pstmt works
tnx a lot
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get More details about PreparedStatement from:

http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html
 
Campbell Ritchie
Sheriff
Pie
Posts: 47313
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find the Java Tutorials easier to read. There is something about prepared statements in the “basics” section.
 
Wendy Gibbons
Bartender
Posts: 1107
Eclipse IDE Oracle VI Editor
 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so if i get this right
let's say my line is

i will have only one pst.set command after it ?

so for every "?" i will have a pst.set ?
 
Wendy Gibbons
Bartender
Posts: 1107
Eclipse IDE Oracle VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
twin yan wrote:ok so if i get this right
let's say my line is

i will have only one pst.set command after it ?

so for every "?" i will have a pst.set ?


almost you do not need to include the ' in your varchar variables, so statements that work with lots of varchars are a LOT LOT easier


so you will need 2 pst.set
pst.setDouble // these are off the top of my head and may not be the correct method names
pst.setString
 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it , but on your example what will be in the pst.setstring ?
the content of bid as a string ?
 
Wendy Gibbons
Bartender
Posts: 1107
Eclipse IDE Oracle VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well in your statement you had id = 'bid', so i presumed it was a string. If it isn't a string use the appropriate method.
 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh i c
thanks a lot for all the help
 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm still having an issue with the preparedstatment :\

the insert of a new row will work just fine but the pst won't change the row , am i doing something bad in there ?
tried debugging and no sql errors
 
Wendy Gibbons
Bartender
Posts: 1107
Eclipse IDE Oracle VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why in this line


are you placing quotes around bid if it is an integer?

this may not be your problem but it is A problem that is worth checking on

it should be:
 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still doing the same thing , what's the diffrence between with ' and without it worked both ways

any ideas on what's wrong with my prepared statment ?
 
Wendy Gibbons
Bartender
Posts: 1107
Eclipse IDE Oracle VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
twin yan wrote:still doing the same thing , what's the diffrence between with ' and without it worked both ways

any ideas on what's wrong with my prepared statment ?


Ok ' are for strings, you are making something convert a striping to an Integer when it isn't required.
The syntax of your prepared statement looks fine to me, so we must start looking at other areas of concern.

I can see a few possible problems
1) does the select return data? You just say result.next Without first checking there is a row returned.
2) if date1 is a database date field just inserting a java default formatted date string could cause problems, much better to also make that a prepared statement then you can use the setDate method which is multi date format safe.
3) what values are returned from the select statement? Pop them into debugging statements to check the values, are they what you expect?
4) in your catch block simply use e.printStackTrace as that shows up red where ever the console output goes. You do know where console output goes?
 
twin yan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
found it !
it was supposed to be


heh thanks for the fresh eyes
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... in which case you should do instead:

Also, I don't know the details of MySQL concurrency model, but allocating a new ID by selecting the maximum ID first and incrementing that may fail in multiuser environments - if two people happen to execute this code at exactly the same time, they may end up using identical ID for the new record. Hopefully a primary key constraint would prevent that, but if you use auto increment column in MySQL, the database can allocate new IDs for you, in a way which is both more effective and safe in multiuser environment.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic