java.sql.SQLException: 字段 'supplier_id' 没有默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26336520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:15:45  来源:igfitidea点击:

java.sql.SQLException: Field 'supplier_id' doesn't have a default value

javamysqlsql

提问by chendurex

I got an error message from this:

我收到一条错误信息:

 java.sql.SQLException: Field 'supplier_id' doesn't have a default value
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)

Everyone can help me ? my database fields are not empty . but i want to get this results:

大家可以帮帮我吗?我的数据库字段不为空。但我想得到这个结果:

insert into xxx(name,password)values('xxx','xxx');and insert into xxx(name,password,man)values('xxx','xxx','xxx');both success (both of that in client is success ,but in java code is error,error code at top title), instead of insert into xxx(name,password)values('xxx','xxx')is false; my mysql jar is mysql-connector-java-5.0.8

insert into xxx(name,password)values('xxx','xxx');并且insert into xxx(name,password,man)values('xxx','xxx','xxx');都成功(在客户端都是成功,但在java代码中是错误,错误代码在顶部标题),而不是insert into xxx(name,password)values('xxx','xxx')错误;我的 mysql jar 是 mysql-connector-java-5.0.8

采纳答案by Deval Khandelwal

The error is self explanatory. Your column supplier_iddoes not have a default value. So during insertion, mysql cannot figure out what to insert in the column supplier_id. You can do either of the three things :-
1. Add a default value to the column supplier_idUsing -

该错误是不言自明的。您的列supplier_id没有默认值。所以在插入过程中,mysql 无法弄清楚要在列中插入什么supplier_id。您可以执行以下三件事中的任何一项:-
1. 将默认值添加到列supplier_id使用 -

ALTER TABLE `xxx` ALTER `supplier_id` SET DEFAULT NULL


2. Supply some value to the supplier_idcolumn during insertion.
3. Add an auto increment to the column and add a primary key to it using the code :-


2.supplier_id在插入期间为列提供一些值。
3. 向列添加自动增量并使用以下代码为其添加主键:-

ALTER TABLE `xxx` CHANGE `supplier_id` `supplier_id` INT(10)AUTO_INCREMENT PRIMARY KEY;

回答by praveen_programmer

You are using table where you have supplier_id column . it does not have default value ,at insertion time this column not getting any value . initialize supplier_id column with some default value , or use auto increment if you are using supplier_id as a primary key.

您正在使用具有 supply_id 列的表。它没有默认值,在插入时此列没有任何值。使用一些默认值初始化 supply_id 列,或者如果您使用 supply_id 作为主键,则使用自动增量。

回答by peter.petrov

To solve this, either supply a value for supplier_idwhen you do the INSERT, or make the column nullablein the DB.

为了解决这个问题,无论是供应的值supplier_id当你这样做的INSERT,或者使列nullable在DB。

回答by Deepak

I added this property

我添加了这个属性

<property name="hbm2ddl.auto">create</property>

into hibernate.cfg.xmlfile, and it worked for me.

进入hibernate.cfg.xml文件,它对我有用。

回答by K81094

There might be two cases.

可能有两种情况。

  1. You have declared the column's default value as NONE and are not not passing any value to it while inserting.

  2. You have declared the column in database, but you have not declared the same in your entity object, which is causing the error.

  1. 您已将该列的默认值声明为 NONE,并且在插入时未向其传递任何值。

  2. 您已经在数据库中声明了该列,但是您没有在实体对象中声明相同的列,这会导致错误。