This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared statement

 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Vani Yama
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic