aspose file tools*
The moose likes JDBC and the fly likes Updating a specific entry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Updating a specific entry" Watch "Updating a specific entry" New topic
Author

Updating a specific entry

twin yan
Greenhorn

Joined: Oct 07, 2012
Posts: 21
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

Joined: May 26, 2003
Posts: 30924
    
158

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?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
twin yan
Greenhorn

Joined: Oct 07, 2012
Posts: 21
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

Joined: Jun 02, 2012
Posts: 187
Get More details about PreparedStatement from:

http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
You may find the Java Tutorials easier to read. There is something about prepared statements in the “basics” section.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

http://beginner-sql-tutorial.com/sql-update-statement.htm
twin yan
Greenhorn

Joined: Oct 07, 2012
Posts: 21
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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Oct 07, 2012
Posts: 21
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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Oct 07, 2012
Posts: 21
oh i c
thanks a lot for all the help
twin yan
Greenhorn

Joined: Oct 07, 2012
Posts: 21
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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Oct 07, 2012
Posts: 21
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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Oct 07, 2012
Posts: 21
found it !
it was supposed to be


heh thanks for the fresh eyes
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

... 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
 
subject: Updating a specific entry