This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes update through prepared statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "update through prepared statement" Watch "update through prepared statement" New topic
Author

update through prepared statement

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
hello,
i have this question. i want to update a data base record. so i am using this coding.
PreparedStatement st_stockaddps=st_stockaddcon.prepareStatement("INSERT INTO inventory VALUES("+st_descriptionst+","+st_ppust+","+st_qtyst+","+st_reorderlevelst+","+st_suppliercodest+","+st_totalcostst+","+st_totalcostst+")WHERE I_Code=?");
st_stockaddps.setString(1,st_itemcodest);
st_stockaddps.executeUpdate();
but the problem is when i execute this statement it gives a sql exception saying "Missing semicolon( at the end of sql statement..
please send a quick reply. thank you.


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Aijaz Soomro
Greenhorn

Joined: Aug 31, 2002
Posts: 17
u should use this pattern

try
{
PreparedStatement pst=con.prepareStatement("Insert Into SavingAccountRecord(ACCOUNTNUMBER,BRANCHCODE,ACCOUNTTYPE,OPENDATE) Values(?,?,?,?)");
pst.setString(1,ACCOUNTNUMBER);
pst.setString(2,BRANCHCODE);
pst.setString(3,ACCOUNTTYPE);
pst.setString(4,OPENDATE);
pst.executeUpdate();
pst.close();
System.out.println(" registered");
}catch(Exception e)
{
try{con.close();}catch(Exception e2){System.out.println("connection not closed");}
System.out.println("could not register");
}
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
If you are trying to Update an existing record rather than create a new record the SQL statement that you are after should look something like :
PreparedStatement st_stockaddps=st_stockaddcon.prepareStatement("UPDATE inventory
SET description = ?,
unitprice = ?,
quantity = ?,
reorderlevel = ?,
suppliercode = ?,
totalcost = ?
WHERE I_Code = ?");
st_stockaddps.setString(1,st_descriptionst);
st_stockaddps.setString(2,st_ppust);
st_stockaddps.setString(3,st_qtyst);
st_stockaddps.setString(4,st_reorderlevelst);
st_stockaddps.setString(5,st_suppliercodest);
st_stockaddps.setString(6,st_totalcostst);
st_stockaddps.setString(7,st_itemcodest);
st_stockaddps.executeUpdate();
[ April 16, 2003: Message edited by: Andy Bowes ]

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: update through prepared statement
 
Similar Threads
Prepared Statement
Prepared statement
Update Using a preparedstatement
How to persist only the value which changed?
Replace à with A using Java code