Easy SQL question
My Java application uses a PreparedStatement to update my Access database.My first field is just an ID column which is set to autoNumber. My question is, when I use the PreparedStatement, the parameter list requires a value for each of my fields, but one of them is an autonumber...so how do I satisfy its demand for a value while letting the autoNumber do its thing at the same time?
Thank you.
What's the statement for? An insert? If so, change your PreparedStatement so it doesn't have the ID as a parameter.
You re-examine this incorrect assumption:
> the parameter list requires a value for each of my fields
It doesn't. (I'm assuming here that this "parameter list" you're talking about is the list of fields in the SQL statement.) The database will give default values to any fields you don't specify. And the default value for an auto-increment field is what you would expect.
> > the parameter list requires a value for each of my
> fields
>
> It doesn't. (I'm assuming here that this "parameter
> list" you're talking about is the list of fields in
> the SQL statement.) The database will give default
> values to any fields you don't specify. And the
> default value for an auto-increment field is what you
> would expect.
Ok, here's my statement:
PreparedStatement updateTable = con.prepareStatement("insert into sales " + "values (?, ?, ?, ?, ?)");
I actually have 6 fields in the DB, the very first one is the ID (autoNumber). The parameter for the ID field, naturally, would go before the first '?'.
When I compile this code the compiler says "number of query values and destination fields are not the same." What am I doing wrong here?
Thanks.
Specify the destination fields...insert into sales (foo, bar, baz) values (?, ?, ?)
I did that. Here's the rest of my code:
PreparedStatement updateTable = con.prepareStatement("insert into sales " + "values (?, ?, ?, ?, ?)");
updateTable.setInt(1, date);
updateTable.setString(2, time);
updateTable.setInt(3, comp_num);
updateTable.setDouble(4, this_comp.getSubTotal());
updateTable.setDouble(5, this_comp.getTotal());
updateTable.executeUpdate();
What I'm trying to say is that I tried doing what you suggested by not specifying a parameter for the autoNumbered ID, but the compiler complained.
> I did that. No, you didn't. Look carefully at the difference between my statement and yours.
Look closely: // mineinsert into sales (foo, bar, baz, bang, zip) values (?, ?, ?, ?, ?)// yoursinsert into sales values (?, ?, ?, ?, ?)
Ok, got it. Thank you.
I was having more trouble compiling it the way you showed me because of syntax errors, but I found out what was wrong...I had the word 'date' in my DB and that's a reserved word and cannot be used the way I was using it. I just thought I'd mention it incase anyone else runs into the same trouble.
> Ok, got it.Great; glad you got it working! Cheers~