package UI;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.toedter.calendar.JDateChooser;
import myLibs.bioJDBC;
import myLibs.bioLibs;
public class Practice implements ActionListener{
JFrame frame;
JButton btn;
JButton insert;
JDateChooser c1;
String strDate;
public Practice() {
frame=new JFrame("Insert Example");
frame.setSize(250,150);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridLayout(2,2));
JLabel datelbl=new JLabel("Date");
c1=new JDateChooser();
c1.setDateFormatString("yyyy-MM-dd");
insert=new JButton("Insert");
insert.addActionListener(this);
frame.add(datelbl);
frame.add(c1);
frame.add(new JLabel());
frame.add(insert);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==insert) {
String date1=((JTextField) c1.getDateEditor().getUiComponent()).getText();
bioLibs bio=new bioLibs();
bio.setDate(date1);
bioJDBC jdbc=new bioJDBC();
boolean result=jdbc.insert(bio);
if(result==true) {
JOptionPane.showMessageDialog(null, "Data stored successfully");
}
else {
JOptionPane.showMessageDialog(null, "Error Ocurred!");
}
}
}
public static void main(String[] args) {
new Practice();
}
}
JDBC Library
package myLibs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
public class bioJDBC {
public boolean insert(bioLibs bio) {
PreparedStatement pstat;
boolean result=false;
String sql="INSERT INTO date_table VALUES (?)";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sampleappb", "root","");
pstat=conn.prepareStatement(sql);
pstat.setString(1, bio.getDate());
pstat.executeUpdate();
pstat.close();
conn.close();
result=true;
}
catch (Exception ex){
System.out.println("Error"+ex.getMessage());
}
return result;
}
}
Getter Setter Library
package myLibs;
import java.sql.Date;
public class bioLibs {
String date;
public bioLibs() {
this.date = "";
}
public bioLibs(String date) {
this.date = date;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "bioLibs [date=" + date + "]";
}
}
Download JDateChooser.jar