Reputation: 147
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");
}
}
Upvotes: 0
Views: 57
Reputation: 5157
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]);
Upvotes: 1