Search code examples
javaswingderbyjdatechooser

How can I save date from swing date chooser to derby tale with date field


I have java swing date chooser and when I passed the date to derby table though prepared insert statement below i got error message refusing the date date type ,in my table i put date type is date . Below the code the second field not accepted by the database pst.setString(2,dateChooser.getText());:

   public void DoSave()    {
           try{   
               String host1 = "jdbc:derby://localhost:1527//accountsdb";
               String uName1="accounts";
    String uPass1="accounts";
    con1=DriverManager.getConnection(host1,uName1 ,uPass1);
    String sql1="INSERT into journal                                             
   (journal_no,journal_date,journal_submain_no,journal_dr
   , journal_cr,journal_desc,journal_user,journal_docno) 
    values (    ?,?,?,?,?,?,?,?)";
    PreparedStatement pst=con1.prepareStatement(sql1);
pst.setString(1,jTjournal_docno.getText().trim() );
pst.setString(2,dateChooser.getText());
 pst.setString(3,jTjournal_submain_no.getText().trim() );
 pst.setString(4,jTjournal_amount.getText() ); //Dr
pst.setString(5,jTjournal_amount.getText() );//Cr
pst.setString(6,jTjournal_desc.getText().trim() );
 pst.setString(7,Frmlogin.myname );
 pst.setString(8,jTjournal_docno.getText().trim() );
 pst.execute();
 rs.close();      
  doClear();
  JOptionPane.showMessageDialog(null, "Saved ");
  new JFband().setVisible(true);
  this.dispose();
  }               
  catch (SQLException ex) {
      System.out.println(ex.getMessage())   ;      
}
   }

I have no idea how to send date to table through date chooser using prepaid statement above please help


Solution

  • Last I have got a solution seems the date chooser I'm using not familiar with my code ,now i was added jcalendar-1.3.3 jar library to my project and replaced my old date choose with JDateChooser and now the code working fine and i can insert records with date to my table as per my below code :

    public void DoSave()    {
               try{   
                   String host1 = "jdbc:derby://localhost:1527//accountsdb";
                   String uName1="accounts";
      String uPass1="accounts";
       con1=DriverManager.getConnection(host1,uName1 ,uPass1);
       String sql1="INSERT into journal (journal_no,journal_date,journal_submain_no,journal_dr,journal_cr,journal_desc,journal_user,journal_docno) values (?,?,?,?,?,?,?,?)";
    
    PreparedStatement pst=con1.prepareStatement(sql1);
    pst.setInt(1,Integer.parseInt(jLvoucher_no.getText().trim()) );
    pst.setString(2,((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
     pst.setInt(3,Integer.parseInt(jTjournal_submain_no.getText().trim()) );
     pst.setInt(4,Integer.parseInt(jTjournal_amount.getText().trim()) ); //Dr
    pst.setInt(5,Integer.parseInt(jTjournal_amount.getText().trim()) );//Cr
    pst.setString(6,jTjournal_desc.getText().trim() );
     pst.setString(7,Frmlogin.myname );
     pst.setString(8,jTjournal_docno.getText().trim() );
     pst.execute();
     JOptionPane.showMessageDialog(null, "Saved");
    
    
    
    
      }               
      catch (SQLException ex) {
          System.out.println(ex.getMessage())   ;      
    }
       }