• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Updating a specific entry

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author & internet detective
Posts: 41998
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get More details about PreparedStatement from:

http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html
 
Marshal
Posts: 79922
395
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find the Java Tutorials easier to read. There is something about prepared statements in the “basics” section.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://beginner-sql-tutorial.com/sql-update-statement.htm
 
twin yan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i c
thanks a lot for all the help
 
twin yan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
found it !
it was supposed to be


heh thanks for the fresh eyes
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic