0

I have a problem in which I think the INSERT statement is wrong as I keep getting the "SQL ERROR". Every time I do the UPDATE statement, it works just fine. I can't seem to find what's wrong with the query.

My database table: emenu_user HAS variables id,username,password,email,contact and food. ALL in which are VARCHAR (for now).

 public void Connection(){

        try{
        Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/emenu?zeroDateTimeBehavior=convertToNull","root","");
        if(connection!=null){
        System.out.println("Database connected");
        }else {System.out.println("Database NOT connected");}


        String name=username_field.getText();
        String pass=password_field.getText();
        String email=email_field.getText();
        String contact=contact_field.getText();

        String query="INSERT INTO emenu_user (username,password,email,contact) VALUES (?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(query);
        statement.setString(1,name);
        statement.setString(2,pass);
        statement.setString(3,email);
        statement.setString(4,contact);


        int set=statement.executeUpdate();

        if(set>0)
        {
            JOptionPane.showMessageDialog(null,"Data Saved");
            homepageMenu homepageMenu= new homepageMenu();
            homepageMenu.setVisible(true);
            dispose();
        }
        else
        {
            JOptionPane.showMessageDialog(null,"ERROR");
        }


    } catch(SQLException e){
            JOptionPane.showMessageDialog(null,"SQL FAILED");
    }

}
10
  • 1
    Do you have a more specific error message? Commented May 4, 2018 at 8:25
  • Is your id field auto-generating or auto-increment? Commented May 4, 2018 at 8:26
  • unfortunately no, the only output i get is build successfull and the SQL ERROR message. id field is auto increment @Thilo Commented May 4, 2018 at 8:29
  • 1
    java.sql.SQLException: Field 'food' doesn't have a default value @Berger Commented May 4, 2018 at 8:32
  • 1
    Is food a not null field? Commented May 4, 2018 at 8:32

1 Answer 1

1

You need to either set field food to null

ALTER TABLE `emenu_user` ALTER `food` SET DEFAULT NULL

or supply some value while insertion

String query="INSERT INTO emenu_user (username,password,email,contact, food) VALUES (?,?,?,?,?)";
    PreparedStatement statement = connection.prepareStatement(query);
    statement.setString(1,name);
    statement.setString(2,pass);
    statement.setString(3,email);
    statement.setString(4,contact);
    statement.setString(5,[SOME_VALUE]);
Sign up to request clarification or add additional context in comments.

1 Comment

yeah, i just realised that. Thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.