This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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
Author

Prepared statement

Ransika deSilva
Ranch Hand

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


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

Joined: May 12, 2003
Posts: 7
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
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Prepared statement
 
It's not a secret anymore!