Mysql&MSAccess: insert into table which have an incremented field

女生的网名这么多〃 提交于 2019-12-12 01:10:14

问题


I am sorry if there is a duplicate but I tried all ways still I can't do the insertion.

I have a table with only two fields ( ID , Name ) When I run this SQL code it must be insert a new record and increment the ID field automatically because it's auto increment but unfortunately don't work . See the trail and errors :

MYSQL :

  1. PreparedStatement pr = con.prepareStatement("insert into names(name) values(?)"); pr.setString(2,"Azad");

    java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

  2. insert into names(id,name) values(?,?)

    java.sql.SQLException: No value specified for parameter 1

MS Access :

  1. insert into names(name) values(?)

java.lang.ArrayIndexOutOfBoundsException: 1

  1. insert into names(id,name) values (?,?)

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect

What's the reason of those errors ? and how to solve it ?

Thanks for suggestions and answers.


回答1:


change pr.setString(2,"Azad"); to pr.setString(1,"Azad");

The first parameter is related to the position of the ? in the prepared statement, not the column index in the table.

java.sql.SQLException: No value specified for parameter 1. This is down to the fact that you have specified two parameters for the query. But have only specified one value, for the second parameter. If "ID" is an auto incremented column then you don't need to pass in a value. If its not then

pr.setString(1,IDVALUE);

pr.setString(2,"Azad");



来源:https://stackoverflow.com/questions/16256977/mysqlmsaccess-insert-into-table-which-have-an-incremented-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!