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 insert to database... 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 "insert to database..." Watch "insert to database..." New topic

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

Joined: Oct 14, 2005
Posts: 19973

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.
I agree. Here's the link:
subject: insert to database...
It's not a secret anymore!