I have an error with jcalendar, it saves 1970-1-1 date i choose a date from jdatechooser textfield but instead it saves an other year.How can i solve this issue,very weird bug i haven't seen anything similar yet.
UPDATE BUTTON CODE
private void updatebtnMouseClicked(java.awt.event.MouseEvent evt) {
//Date date=NextServiceDate1.getDate();
//String serviceDate=String.format("%1$tY-%1$tm-%1$td", date);
String currDate=null;
String cost=null;
String serviceDate=null;
try{
String name=txtname.getText();
String surname=txtsurname.getText();
String street=txtstreet.getText();
String city=txtcity.getText();
String mobile=txtmobile.getText();
String phone=txtphone.getText();
conn=con.getConn();
String Getsql="SELECT * FROM customers WHERE id="+lblID.getText();
pst=conn.prepareStatement(Getsql);
rs=pst.executeQuery();
while(rs.next()){
currDate=rs.getString("next_service");
cost=rs.getString("cost");
}
if("".equals(currDate)){ //CHECK IF THE DATE IN DATABASE IS EMPTY
serviceDate=null; //IF TRUE THEN ASSIGN TO CURRENT DATE EMPTY VALUE
}else if(NextServiceDate.getDate()!=null){ //ELSE
//String date=((JTextField)NextServiceDate.getDateEditor().getUiComponent()).getText(); //GET THE VALUE FROM JDATECHOOSER FORMAT DATE TO STRING
Date datef=NextServiceDate.getDate();
// SimpleDateFormat formatter=new SimpleDateFormat("YYYY-MM-DD");
serviceDate=String.format("%1$tY-%1$tm-%1$td",datef);
JOptionPane.showConfirmDialog(rootPane, serviceDate);
}
if(cost==null) { //IF COST FIELD IN DATABASE IS EMPTY
cost=null; //THEN ASSIGN TO COST NULL VALUE
}else if(txtcost.getText()!=null){ //ELSE IF COST TEXTFIELD IN NOT NULL
cost=txtcost.getText(); //THEN ASSIGN THE COST WITH THE TEXTFIELD VALUE
}
String Updatesql="UPDATE customers SET name='"+name+"',surname='"+surname+"',street='"+street+"',city='"+city+"',mobile='"+mobile+"',phone='"+phone+"',next_service='"+serviceDate+"',cost='"+cost+"' WHERE id='"+lblID.getText()+"'";
int rows=pst.executeUpdate(Updatesql);
JOptionPane.showConfirmDialog(rootPane,"Updated","Message",JOptionPane.DEFAULT_OPTION);
}catch(SQLException e){
JOptionPane.showConfirmDialog(rootPane, e,"Message",JOptionPane.DEFAULT_OPTION);
}
con.CloseMySQLConn(pst, conn);
AllCustomers();
}
TABLE MOUSE CLICK CODE
private void Customers_TableMouseClicked(java.awt.event.MouseEvent evt) {
try{
int row=Customers_Table.getSelectedRow();
String table_click=(Customers_Table.getModel().getValueAt(row, 0).toString());
String sql="select * from customers where id="+table_click;
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
lblID.setText(rs.getString("id"));
txtname.setText(rs.getString("name"));
txtsurname.setText(rs.getString("surname"));
txtstreet.setText(rs.getString("street"));
txtcity.setText(rs.getString("city"));
if((rs.getString("next_service")!=null)){
NextServiceDate.setDateFormatString(rs.getString("next_service"));
}
txtmobile.setText(rs.getString("mobile"));
txtphone.setText(rs.getString("phone"));
txtcost.setText(rs.getString("cost"));
}
}catch(SQLException e){
JOptionPane.showMessageDialog(rootPane, e);
}
}
In the app i save customer personal info and the date and cost of the next service.So i convert the jdatechooser date to (yyyy-mm-dd) so i can compare with the current system date and to show a message which customers must be informed for their car service. It's a small app just to store my customers, i try hard to solve the problems that appear. Thanks in advance.