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.

[408 byte] By [scottd72@aim.coma] at [2007-10-2 11:59:43]
# 1
Just don't set it.
MartinHilperta at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 2
What's the statement for? An insert? If so, change your PreparedStatement so it doesn't have the ID as a parameter.
yawmarka at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 3

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.

DrClapa at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 4

> > 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.

scottd72@aim.coma at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 5
Specify the destination fields...insert into sales (foo, bar, baz) values (?, ?, ?)
yawmarka at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 6

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.

scottd72@aim.coma at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 7
> I did that. No, you didn't. Look carefully at the difference between my statement and yours.
yawmarka at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 8
Look closely: // mineinsert into sales (foo, bar, baz, bang, zip) values (?, ?, ?, ?, ?)// yoursinsert into sales values (?, ?, ?, ?, ?)
yawmarka at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 9

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.

scottd72@aim.coma at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...
# 10
> Ok, got it.Great; glad you got it working! Cheers~
yawmarka at 2007-7-13 8:14:49 > top of Java-index,Java Essentials,Java Programming...