Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

sql

mysql

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

like image 494
chendurex Avatar asked Oct 13 '14 09:10

chendurex


3 Answers

The error is self explanatory. Your column supplier_id does 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_id Using -

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


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

ALTER TABLE `xxx` CHANGE `supplier_id` `supplier_id` INT(10)AUTO_INCREMENT PRIMARY KEY;
like image 156
Dev Avatar answered Oct 01 '22 19:10

Dev


To solve this, either supply a value for supplier_id when you do the INSERT, or make the column nullable in the DB.

like image 45
peter.petrov Avatar answered Oct 01 '22 19:10

peter.petrov


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.

like image 44
K81094 Avatar answered Oct 01 '22 17:10

K81094