如何在java eclipse中通过jdatechooser更新日期

时间:2017-04-23 11:10:03

标签: java eclipse sqlite jdatechooser

当我尝试更新日期时,jdatechooser日期无法保存在数据库中,在数据库日期中不显示只显示日期格式,如我在MM / dd / yyyy保存的数据库日期更新02/08/2014?我正在使用sqlite数据库和eclipse juno

            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 个答案:

答案 0 :(得分: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());

接受你的选择......