2014-04-20 34 views
1

我正在使用Netbeans和Microsoft Access。在Microsoft Access中,我有5個數據字段,其中之一是ID這是自動編號,但是當我使用Java中的查詢時。如何不將數據字段發送到數據庫Java和Microsoft Access

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

它顯示了錯誤:

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

但問題是,最後一個字段是ID這是自動編號,但我有一個查詢發送到我應該補充何種查詢數據庫到最後讓它工作而不干擾數據庫中的ID字段?

回答

1

如果使用Insert語句的這種格式,而不是當前的語句這將是有用的:

INSERT INTO <table name> (<list of field name>) VALUES (<list of field values>) 

Using this format is useful when you have an IDENTITY or AUTO-NUMBER field, and when you have some fields with default values.

這樣的猜測;你的陳述應該是這樣的:

"INSERT INTO (name, size, quantity, price) stockDB VALUES('"+name+"','"+size+"','"+quantity+"','"+price+"')" 
相關問題