File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Prepared statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Prepared statement" Watch "Prepared statement" New topic

Prepared statement

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
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+")");

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Vani Yama

Joined: May 12, 2003
Posts: 7
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+")");

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

Vani Yama
I agree. Here's the link:
subject: Prepared statement
It's not a secret anymore!