21 April 2016

Jcalendar Insert Select with Eclipse to Database mysql workbench


STEP 1: Download jcalendar-1.4.zip

STEP 2: Open jcalendar-1.4.zip


STEP3: Extract to workspace  or every where
C:\Users\..NamePc..\workspace

STEP4: Palette manager..










OK

Begin create the program

CREATE Database


Create 


Ctrl + N



date.java
package DBImages;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JButton;
import javax.swing.JFrame;

import com.toedter.calendar.JDateChooser;

import Connect.DBConnection;
import javax.swing.JLabel;

public class date {

 private JFrame frame;
 private Connection conn;
 private PreparedStatement stmt;
 private ResultSet rs;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     date window = new date();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */
 public date() {
  initialize();
  try {
   conn = DBConnection.getConnection();
  } catch (Exception e) {
   // TODO: handle exception
  }
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 376, 194);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);

  JDateChooser dateChooser = new JDateChooser();
  dateChooser.setDateFormatString("dd,MM, yyyy");
  dateChooser.setBounds(48, 28, 145, 23);
  frame.getContentPane().add(dateChooser);

  JLabel lblDate = new JLabel("");
  lblDate.setBounds(48, 82, 145, 19);
  frame.getContentPane().add(lblDate);

  JButton btnAdd = new JButton("Add");
  btnAdd.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {

    Date date = dateChooser.getDate();
    String dateInput = new SimpleDateFormat("yyyy-MM-dd").format(date);

//    lblDate.setText(dateInput);
    
    String sql = "INSERT INTO `manager`.`date` (`date`) VALUES  (?)";
    try {
     stmt = conn.prepareStatement(sql);
     stmt.setString(1, dateInput);
     stmt.executeUpdate();
    } catch (Exception e) {
     // TODO: handle exception
    }

   }
  });
  btnAdd.setBounds(203, 28, 89, 23);
  frame.getContentPane().add(btnAdd);

  JButton btnShow = new JButton("Show");
  btnShow.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    String sql = "SELECT * FROM manager.date";
    try {
     stmt = conn.prepareStatement(sql);
     rs = stmt.executeQuery();
     while(rs.next()){
      String date = new SimpleDateFormat("yyyy-MM-dd").format(rs.getDate("date"));
      lblDate.setText(date);
     }
     
    } catch (Exception e) {
     // TODO: handle exception
    }
   }
  });
  btnShow.setBounds(203, 78, 89, 23);
  frame.getContentPane().add(btnShow);

 }
}


0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang