1

For a testing purposes i created a form with 2 jTextFields, button and a table. I'm trying to add data to database. But i seem to run into a problem. When i press the button, it returns fail message because the data haven't been added to database. I'd appreciate any help. So here's what i'm doing. I created ConnectionConfiguration class to simplify code:

public class ConnectionConfiguration {
    public static Connection getConnection() {
    Connection connection = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Connection Success");
    } catch(ClassNotFoundException e) {
        System.out.println("Connection Failed");
    }
    try {
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/systemnew?zeroDateTimeBehavior=convertToNull", "root","123456");
        System.out.println("Database Connected");
    } catch (SQLException se){
        System.out.println("No Database" + se);
    }
    return connection;
    }   
}

Connection is always success and database always connects. The error message states that my mistake is here (at systemnew.UpdateDatabase.add).Here's add method from UpdateDatabase class:

public boolean add(String field1, String field2) {
    try {
        Connection conn = ConnectionConfiguration.getConnection();
        PreparedStatement ps = conn.prepareStatement("INSERT INTO newtable(field1,field2) VALUES('"+field1+"','"+field2+"')");
        ps.executeUpdate();
        return true;
    } catch(Exception ex){
        ex.printStackTrace();
    }
    return false;
}

and here's the code for button that should potentially add data to database:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(new UpdateDatabase().add(jTextField1.getText(),jTextField2.getText())){
        JOptionPane.showMessageDialog(null, "Added successfully!");
    } else {
        JOptionPane.showMessageDialog(null, "Record has not been added!");
    }
}  

Error

Connection Success
Database Connected
java.sql.SQLException: Field 'tblid' doesn't have a default value
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
    at systemnew.UpdateDatabase.add(UpdateDatabase.java:38)
2
  • 1
    post the complete error message Commented Sep 8, 2016 at 0:54
  • @sidgate pastebin.com/raw/WZwdh5xz Line "at systemnew.UpdateDatabase.add(UpdateDatabase.java:38)" is selected. Commented Sep 8, 2016 at 1:05

2 Answers 2

1

In you table newtable there is a field called tblid which you are not assigning a value to so you get the following error

Field 'tblid' doesn't have a default value

BTW it would be safer to use PreparedStatement in order to avoid possible Sql Injection

Also consider this answer to find out about auto incrementing id fields

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks to you too. If i may ask, where am i not using PreparedStatement?
Sorry you are - my bad. Try using replaceable parameters ? and then setString etc.
you mean something like this? PreparedStatement ps = conn.prepareStatement("INSERT INTO newtable(field1,field2) VALUES(?,?)"); ps.setString(1,textField.getText()); etc.
Yep, looks good. Maybe want to trim your getText value though
0

The error tells the specific issue

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

Your table newtable seems to have non-null tblid column for which you are not specifying any value during insert. You may want to mark it as auto increment

1 Comment

I seem to be having the biggest tunnel vision. Thanks.

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.