Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to insert date value from JDateChooser to MySQL database table?

 


GUI.java

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
Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.