• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Prepared statement

 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello;
I am writting a stock control system. in there to enter new stock i am using below code. the problem is that it works fine when i enter all "numeric values". but if i enter a letter for example 'a'. it gives a sql exception saying "too few parameters. expected 1". could you please give me a solution for this. i am using microsoft access data base.

PreparedStatement st_stockaddps=st_stockaddcon.prepareStatement("INSERT INTO inventory VALUES("+st_itemcodest+","+st_descriptionst+","+st_ppust+","+st_qtyst+","+st_reorderlevelst+","+st_suppliercodest+","+st_totalcostst+","+st_totalcostst+")");
st_stockaddps.executeUpdate();
thanks
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think you need to prefix string values with single quote ('), like I am doing for 'st_descriptionst' in the following line ...
PreparedStatement st_stockaddps=st_stockaddcon.prepareStatement(
"INSERT INTO inventory VALUES("+st_itemcodest+","+"'"+st_descriptionst+"'"+","+st_ppust+","+st_qtyst+","+st_reorderlevelst+","+st_suppliercodest+","+st_totalcostst+","+st_totalcostst+")");
OR

String sqlStr = new String ("INSERT INTO inventory VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
PreparedStatement st_stockaddps = st_stockaddcon.prepareStatement(sqlStr);
st_stockaddps.setInt(1, st_itemcodest);
st_stockaddps.setString(2, st_descriptionst);
//set all the parameters in the same way
st_stockaddps.executeUpdate();

Thanks,
Vani Yama
 
reply
    Bookmark Topic Watch Topic
  • New Topic