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

I am trying to insert data into arrivaltimes tables but I am getting the following error:

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

stt.execute("CREATE TABLE IF NOT EXISTS stops"
            + "(stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
            + " name varchar(30) NOT NULL, "
            + " route INT(11) NOT NULL, "
            + " lat double(10,6) NOT NULL, "
            + " longi double(10,6)NOT NULL) " );

    stt.execute("INSERT INTO stops(name, route, lat, longi) values"
            + "('blabla', '1', '93.838039', '15.700440' ),"
            + "('backery', '9', '98.868863', '19.665438' )" );

    stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL PRIMARY KEY,"
            +  " weekday VARCHAR(20) NOT NULL,"
            + "arrivaltime time NOT NULL,"
            + " stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )" );
    //The error appears in this execution statement.
    stt.execute("INSERT INTO arrivaltimes(weekday, arrivaltime) values"
            + "('mon-fri', '05:30' ),"
            + "('mon-fri', '06:07' )" );

Answers


You are missing AUTO INCREMENT for Primary Key in arrivaltimes table. Just need to add AUTO_INCREMENT while creating table

stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL  AUTO_INCREMENT PRIMARY KEY,"
            +  " weekday VARCHAR(20) NOT NULL,"
            + "arrivaltime time NOT NULL,"
            + " stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )" );

Need Your Help

Commit transaction after exception - undo setRollbackOnly

java-ee transactions ejb-3.0 eclipselink

Within my method with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) there can sometimes happen a org.eclipse.persistence.exceptions.DatabaseException due to an SQLException. I catch ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.