James Angelo James Angelo - 2 months ago 6
Java Question

Adding data to MySQL database

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)

Answer

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