Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update through prepared statement

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aijaz Soomro
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic