Reputation: 33
I am unable to insert selected date from jdate Picker to mysql database I need a help please because i have tried by best but am getting exception. The code run successfully but am getting this exception when clicking ADD button.
java.text.ParseException: Unparseable date: "null"
at java.text.DateFormat.parse(DateFormat.java:366)
Here is the complete source code which run successfully but fire out exception when ADD button pressed
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.text.DateFormat;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;
import java.sql.*;
import java.util.Date.*;
import java.util.Date;
public class Testd {
Connection con=null;
Statement st;
ResultSet rs;
PreparedStatement pst = null;
JButton btn;
String datee;
public static void main(String[] args) {
new Testd();
}
public Testd() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(null);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
btn = new JButton("Add");
btn.setBounds(280,350,200,30);
UtilDateModel model = new UtilDateModel();
Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
datePicker.setBounds(220,350,120,50);
Date selectedDate = (Date) datePicker.getModel().getValue();
datee=selectedDate+"";
add(datePicker);
add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnSaveActionPerformed(evt);
}});
}
}
private void btnSaveActionPerformed(ActionEvent evt) {
try{
String text = datee;
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date myDate = formatter.parse(text);
java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
String sql = "INSERT INTO Invoice"
+"(Issuedate)"
+"VALUES (?)";
con = DriverManager.getConnection("jdbc:mysql://localhost/Sales"+"?useSSL=false","root","3000");
pst.setDate(1,sqlDate);
}
catch(SQLException | HeadlessException ex){
JOptionPane.showMessageDialog(null,"ERROR UNABLE TO INSERT ");
JOptionPane.showMessageDialog(null,ex);
}
catch (ParseException ex) {
ex.printStackTrace();
}
}
public class DateLabelFormatter extends AbstractFormatter {
private String datePattern = "yyyy-MM-dd";
private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);
@Override
public Object stringToValue(String text) throws ParseException {
return dateFormatter.parseObject(text);
}
@Override
public String valueToString(Object value) throws ParseException {
if (value != null) {
Calendar cal = (Calendar) value;
return dateFormatter.format(cal.getTime());
}
return "";
}
}
}
Upvotes: 2
Views: 3498
Reputation: 2279
just as your exception inform. You are passing null value to the formatter. Ensure that
This line Date selectedDate = (Date) datePicker.getModel().getValue();
can return correct date value to Date
And after confirming it can return date value as expected. Change your code by refe code need to delete to prevent the error.
public class Testd {
...
Date datee; // Change this variable type
...
public class TestPane extends JPanel {
public TestPane() {
...
datee = (Date) datePicker.getModel().getValue(); // change this line
// datee=selectedDate+""; // Remove this line
...
pst=con.prepareStatement(sql);
...
}
}
private void btnSaveActionPerformed(ActionEvent evt) {
try{
// String text = datee; // Remove this line
// DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); // Remove this line
// Date myDate = formatter.parse(text); // Remove this line
java.sql.Date sqlDate = new java.sql.Date(datee.getTime()); // change this line
...
}
catch(SQLException | HeadlessException ex){
...
} catch (ParseException ex) {
ex.printStackTrace();
}
...
}
}
Upvotes: 2