2017-04-23 53 views
1

當我嘗試更新日期由jdatechooser日期不能保存在數據庫中,在數據庫中日期不顯示只顯示日期格式像我更新數據庫日期保存02/08/2014由MM/dd/yyyy?我使用SQLite數據庫和eclipse JUNO如何在java eclipse中通過jdatechooser更新日期

  try{ 
       String t10= comboBoxGen.getSelectedItem().toString(); 
       String JD= dateChooser.getDateFormatString(); 

       String t1= textFieldID.getText(); 
       String t2= textFieldFN.getText(); 
       String t3= textFieldLN.getText(); 
       String t4= textFieldCN.getText(); 
       String t5= textFieldAge.getText(); 
       String t6= textFieldDep.getText(); 
       String t7= textFieldMob.getText(); 
       String t8= textFieldSal.getText(); 
       String BD= dateChooser1.getDateFormatString(); 
      // String t9= textFieldBD.getText(); 
      // String t10= textFieldGen.getText(); 
       String t11= textFieldAdd.getText(); 
       String t12= textFieldPos.getText(); 
      // String t13= textFieldJD.getText(); 

       if(t1.length()>4 || t1.length()==0 || t2.length()==0 || t3.length()==0 || t4.length()==0 || t5.length()==0 || t6.length()==0 || t7.length()==0 || t8.length()==0 || BD.length()==0 || t10.length()==0 || t11.length()==0 || t12.length()==0 || JD.length()==0){ 
        JOptionPane.showMessageDialog(null, "Please fill the fields or correct filled","Error", JOptionPane.ERROR_MESSAGE); 
       } 
       else{ 
        String query3="Update Teachers set TeacherID='"+textFieldID.getText()+"', FirstName='"+textFieldFN.getText()+"', LastName='"+textFieldLN.getText()+"', CNICNO='"+textFieldCN.getText()+"', Age='"+textFieldAge.getText()+"', Salary='"+textFieldSal.getText()+"', MobileNo='"+textFieldMob.getText()+"', Gender='"+t10+"', Department='"+textFieldDep.getText()+"', Position='"+textFieldPos.getText()+"', Address='"+textFieldAdd.getText()+"', JoinDate='"+JD+"', BirthDate='"+dateChooser1.getDateFormatString()+"' where TeacherID='"+textFieldID.getText()+"' ";  
        PreparedStatement pst= connection.prepareStatement(query3); 

       int action = JOptionPane.showConfirmDialog(null, "Do you want Update this Data?","Update Confirmation",JOptionPane.YES_NO_OPTION); 
       if (action==0){ 
       pst.execute(); 
       } 
       JOptionPane.showMessageDialog(null, "Data Update"); 

       pst.close(); 

       } 
      }catch(SQLException e){ 
       e.printStackTrace(); 
      } 
     } 
    }); 
    btnUpdateData.setBounds(399, 443, 105, 23); 
    contentPane.add(btnUpdateData); 

回答

1

需要調用JDateChooser.getDate()獲得輸入的日期。當然,這會返回java.util.Date,所以如果這是您真正想要的,您需要採取一些額外的步驟才能將日期轉換爲字符串形式。

這裏有一個辦法做到這一點:

java.util.Date dt = dateChooser1.getDate(); 
String BD = "" + 
    (dt.getMonth() + 1) + 
    "/" + 
    dt.getDate() + 
    "/" + 
    (1900+ dt.getYear(); 

這裏有一個漂亮的方式:

String BD = (new java.text.SimpleDateFormat("MM/dd/yyyy")) 
    .format(dateChooser1.getDate()); 

隨你挑......