File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes insert to database... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "insert to database..." Watch "insert to database..." New topic
Author

insert to database...

Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
how do add string stored in variable to database?
my code looks like this

i get this error message;
sql error![Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 4.

please help me out with the insert statement
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

The driver is simply saying that the table doesn't have columns named sprt, sop, msc, or kid. That's what you told it to insert, data from those columns. It doesn't know anything about those variables in your Java code which happen to have the same names, nor should it.

Here's what you should do instead:

The four question marks in the PreparedStatement act like places where you can plug in values, and the setString(n) method is used to plug values into those places. There are also setInt(n) and setDate(n) methods and several others, which mean you don't need to worry about how to format a date for an SQL query, or what to do about quotes in your string data, or any other annoying formatting issues.
Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
thanks... it worked.
 
jQuery in Action, 2nd edition
 
subject: insert to database...