当我尝试通过jdatechooser日期更新日期时,不能保存在数据库中时,在数据库日期中不仅显示显示日期格式,例如我更新02/08/2014在数据库日期中保存的数据库日期mm/dd/yyyy?我正在使用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);
您需要致电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());
选择您的选择...