user3551620
user3551620

Reputation: 199

How to ignore a field in Database with java sql query Netbeans

I have 5 rows in MS Access database one of them is Serial Number whose property is auto number I am trying to pass this query to the database to insert.

int s= sta.executeUpdate("INSERT INTO stockDB VALUES('"+name+"','"+size+"','"+quantity+"','"+price+"')");

In the database the Serial Number is the first column then the name,size,quantity and price.

When I am trying to do the insert i get this error:

[Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.

I know i should put something in the first but what should i put it there? I tried to put the Serial Number at the last but the error was the same. What should i put in the query so that the sql accepts it but still genrates auto number in the database?

Upvotes: 1

Views: 1001

Answers (3)

Amit
Amit

Reputation: 288

try using different format of the query -

int s = sta.executeUpdate("insert into stockDB ('name', 'size', 'quantity', 'price') values('" + name + "','" + size + "','" + quantity + "','" + price + "')");

Upvotes: 1

Mudassir Hasan
Mudassir Hasan

Reputation: 28771

You need to specify Serial number field as AUTO_INCREMENT to automatically generate value for the field on inserting eecord in table.

Then specify field names in INSERT query and supply values.

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13854

your sql query is wring do like this

int s= sta.executeUpdate("INSERT INTO stockDB(name,size,quantity,price) VALUES('"+name+"','"+size+"','"+quantity+"','"+price+"')");

you need not to insert serial number manually because its auto increment but you have to specify the rest of the column names and values to be inserted and serial number value will be inerted automatically as its auto increment

Upvotes: 3

Related Questions