1

Below is my code for saving dates to the database.

codes getting entered data from text fields and jdatechoosers,

String id = txt_id.getText();
    String name = txt_name.getText();
    String sname = txt_sname.getText();
    String bday=((JTextField)bday_chooser.getDateEditor().getUiComponent()).getText();
    String wday =((JTextField)wday_chooser.getDateEditor().getUiComponent()).getText();

code for inserting to db,

String sql = "Insert into employeeinfo (EmployeeID,Name,Surname,BirthDate,WorkStartedDate,Gender,Image)values (?,?,?,?,?,?,?)";

            pst=conn.prepareStatement(sql);

            pst.setString(1,id);
            pst.setString(2,name );
            pst.setString(3,sname );
            pst.setString(4,bday );
            pst.setString(5,wday );
            pst.setString(6, Gender);
            pst.setBytes(7, emp_img);

            pst.execute();

But when executing shows an exception, as follows;

    Aug 06, 2015 10:46:59 AM com.bit.project.UserManagementNewUser save_btnActionPerformed
SEVERE: null
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '' for column 'BirthDate' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3833)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1203)
    at com.bit.project.UserManagementNewUser.save_btnActionPerformed(UserManagementNewUser.java:557)
    at com.bit.project.UserManagementNewUser.access$300(UserManagementNewUser.java:38)
    at com.bit.project.UserManagementNewUser$4.actionPerformed(UserManagementNewUser.java:305)
hinata
  • 385
  • 1
  • 3
  • 12
  • What is your table structure? What is the datatype of **BirthDate** column? – Manish Kothari Aug 06 '15 at 05:31
  • @user3145373ツ Depending on the column type, OP might require a `String`, `java.sql.Timestamp`, `java.sql.Date`, and so on... No way of telling. – Robby Cornelissen Aug 06 '15 at 05:33
  • @ManishKothari As the error message says ` Incorrect date value`. So the column must be a date type. – Jens Aug 06 '15 at 05:36
  • You need to pass Timestamp instance. Show your table schema. If table type is date time for that field then you can directly use `now()` function also. – user3145373 ツ Aug 06 '15 at 05:37
  • @Jens agreed, my bad, i missed it. – Manish Kothari Aug 06 '15 at 05:37
  • Don't save date/time values as String, use the database date/time column type and setDate/Time methods of PreparedStatement – MadProgrammer Aug 06 '15 at 05:38
  • Yes. BirthDate can be Null in my database. But how to set it Null? @Naman Gala – hinata Aug 06 '15 at 05:39
  • @MadProgrammer Type is Date in the database But I'm trying to save to the database as String. IS that concept wrong? IF wrong how can I save a Date as Null (when not given by user )? – hinata Aug 06 '15 at 05:43
  • @hinata So you're saying your trying to put a square peg in a round hole? You can save a null Date by passing the setDate method a null value – MadProgrammer Aug 06 '15 at 05:55
  • @hinata You can save null values to database if you change date column in your database to accept nulls. – Kiki Aug 06 '15 at 05:56

0 Answers0