SQLDataException从jtable模型插入日期时,日期/时间的字符串表示的语法不正确

时间:2016-03-01 20:13:28

标签: java swing jtable derby jdatechooser

我正在向表模型中添加数据,而表模型又将传递给数据库。我添加的一些字段是日期对象,如下面的代码所示。

String ref = cboReferenceNumber.getSelectedItem().toString();
    if(!txtAction.getText().equals("")  ){
          DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
          java.util.Date date = new java.util.Date();
          String action = txtAction.getText();
          String hierachyOfControl =      (String)cboHierachyOfControl.getSelectedItem();
          String responsiblePerson = (String)cboName.getSelectedItem();
          java.util.Date dueDate = dcDueDate.getDate();
          java.sql.Date sqlDueDate = new java.sql.Date(dueDate.getTime());
          String status = txtStatus.getText();
          Object[] newRow = {action,hierachyOfControl,responsiblePerson,sqlDueDate,status};
                if(dcDueDate.getDate().getTime() <= date.getTime()){
                      JOptionPane.showMessageDialog(Corrective.this, "Due date can not be less or equal to today's date");}
                else{
        model.addRow(newRow);
    }
    }else{
        JOptionPane.showMessageDialog(Corrective.this, "Please enter action required for"+" "+ref);
    }

然后我使用下面的代码将创建的模型添加到数据库,但是日期列在我的问题标题上抱怨错误消息。

        String sql = "insert into CorrectiveAction(ReferenceNumber,Action,Hierachy"+
            ",ResponsiblePerson,DuteDate,Status)values(?,?,?,?,?,?)";
    String reference = cboReferenceNumber.getSelectedItem().toString();
    TableModel tm = jTable1.getModel();
    try {
        Connection con = DbConnection.dbConnection();
        PreparedStatement pst = con.prepareStatement(sql);
        for(int tableRow = 0; tableRow < tm.getRowCount(); tableRow++){
            for(int col = 0; col < tm.getColumnCount(); col++){
                Object val = tm.getValueAt(tableRow, col);
                pst.setObject(col+1, val);
                pst.setString(6, reference);
            }
            pst.addBatch();
        }
        pst.executeBatch();

        pst.executeBatch();
        JOptionPane.showMessageDialog(this, "Record successfully saved..!");
    }
    catch (SQLException | HeadlessException | ClassNotFoundException e){
        Logger.getLogger(Corrective.class.getName()).log(Level.SEVERE, null, e);
    }

0 个答案:

没有答案