将日期保存到MySQL数据库

时间:2015-08-06 05:29:05

标签: java mysql

以下是我将日期保存到数据库的代码。

从文本字段和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();

插入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();

但是当执行显示异常时,如下所示;

    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)

0 个答案:

没有答案