0

I'm adding data to a table model which in turn i will pass to the database.amoung some of the fields am adding is a date object as shown by the code below.

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);
    }

i then use the code below to add the created model to a database but the date column complains of the error message on my question header.

        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);
    }
Herb21
  • 365
  • 1
  • 6
  • 12
  • Use a `java.sql.TimeStamp` or `java.sql.Date` to represent the date values (assuming the database is setup to use the date type columns) and let the driver/database dela with it – MadProgrammer Mar 01 '16 at 21:04
  • Are you looking for examples of how to work with DATE and TIMESTAMP columns using JDBC? Here's a nice link: http://www.herongyang.com/JDBC/Derby-DML-Date-Timestamp.html You can also check the Derby docs: https://db.apache.org/derby/docs/10.12/ref/rrefsqlj27620.html and https://db.apache.org/derby/docs/10.12/ref/rrefsqlj18730.html – Bryan Pendleton Mar 01 '16 at 23:03
  • @MadProgrammer if you check my code you can see that i create my date with java.util.Date then convert it to a java.sql.Date but i still get the prob. Kindly look at my code at let me know where am going wrong – Herb21 Mar 02 '16 at 05:57

0 Answers0