严重:java.sql.SQLException:字段'columnname'没有默认值

时间:2016-08-23 15:04:57

标签: java mysql prepared-statement

 JSONArray newdata = new JSONArray(tabledata);

 int prep = 0;
 String add_sie = "INSERT INTO " + table + " " +
    "(" + yearcolumn + "," + orgcolumn + "," + suborgcolumn + "," + amount + "," + createdbycolumn + "," + createddatecolumn + "," + statuscolumn + " , " + createdtimecolumn + " )" +
    " VALUES (?,?,?,?,?,?,?,?)";

 preparedStatement = conn.prepareStatement(add_sie);
 for (int i = 0; i < newdata.length(); i++) {

    preparedStatement.setString(1, newdata.getJSONObject(i).get("year").toString());
    preparedStatement.setString(2, newdata.getJSONObject(i).get("org").toString());
    preparedStatement.setString(3, newdata.getJSONObject(i).get("sub_org").toString());
    preparedStatement.setString(4, newdata.getJSONObject(i).get("sub_org_amt").toString());
    preparedStatement.setString(5, af_mas_bi_createdby);
    preparedStatement.setString(6, af_mas_bi_datecreated);
    preparedStatement.setString(7, af_mas_bi_timecreated);
    preparedStatement.setString(8, "approved");
    //preparedStatement.executeUpdate();

    //preparedStatement.executeBatch();

 }
 preparedStatement.executeBatch();

我收到错误Severe: java.sql.SQLException: Field 'abap_id' doesn't have a default value,这是我在数据库中放置的结构

enter image description here

为什么我会在该ID中出现错误,即使它是自动增量并且我没有将其插入具有特定值的插入

1 个答案:

答案 0 :(得分:0)

 String add_sie = "INSERT INTO " + table + " " +
"(abap_id," + yearcolumn + "," + orgcolumn + "," + suborgcolumn + "," + amount + "," + createdbycolumn + "," + createddatecolumn + "," + statuscolumn + " , " + createdtimecolumn + " )" +
" VALUES (NULL,?,?,?,?,?,?,?,?)";

重要提示!如果您未将值设置为null或NULL并手动插入值,则不会更新 AUTO_INCREMENT 种子。如果AUTO_INCREMENT列具有UNIQUE约束或 PRIMARY KEY ,则可能导致唯一约束违规错误