26 April 2016

Insert images Blob database in mysql Eclipse Java swing




ChiTiet.java
package ThongTin;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.toedter.calendar.JDateChooser;

import DBConnection.DBConnection;

public class ChiTiet {

 private JFrame frmChiTiet;
 private JTextField txtName;
 private JTextField txtAge;
 private JTextField txtClass;
 private JTextField txtAddress;

 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 {
     ChiTiet window = new ChiTiet();
     window.frmChiTiet.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

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

 /**
  * Initialize the contents of the frame.
  */
 String filename =null;
 BufferedImage bi;
 private JTextField path;
 private void initialize() {
  frmChiTiet = new JFrame();
  frmChiTiet.setTitle("Them thong tin vao danh sach");
  frmChiTiet.setBounds(100, 100, 517, 331);
  frmChiTiet.getContentPane().setLayout(null);

  txtName = new JTextField();
  txtName.setBounds(351, 22, 129, 20);
  frmChiTiet.getContentPane().add(txtName);
  txtName.setColumns(10);

  txtAge = new JTextField();
  txtAge.setBounds(351, 56, 129, 20);
  frmChiTiet.getContentPane().add(txtAge);
  txtAge.setColumns(10);

  txtClass = new JTextField();
  txtClass.setBounds(351, 87, 129, 20);
  frmChiTiet.getContentPane().add(txtClass);
  txtClass.setColumns(10);

  txtAddress = new JTextField();
  txtAddress.setBounds(351, 157, 129, 20);
  frmChiTiet.getContentPane().add(txtAddress);
  txtAddress.setColumns(10);

  JLabel lblTen = new JLabel("Ten");
  lblTen.setBounds(233, 25, 75, 14);
  frmChiTiet.getContentPane().add(lblTen);

  JLabel lblTuoi = new JLabel("Tuoi");
  lblTuoi.setBounds(233, 59, 75, 14);
  frmChiTiet.getContentPane().add(lblTuoi);

  JLabel lblLop = new JLabel("Lop");
  lblLop.setBounds(233, 90, 75, 14);
  frmChiTiet.getContentPane().add(lblLop);

  JLabel lblNgaySinh = new JLabel("Ngay sinh");
  lblNgaySinh.setBounds(233, 121, 75, 14);
  frmChiTiet.getContentPane().add(lblNgaySinh);

  JLabel lblAddress = new JLabel("Address");
  lblAddress.setBounds(233, 160, 75, 14);
  frmChiTiet.getContentPane().add(lblAddress);
  
  JPanel panel = new JPanel();
  panel.setBorder(new TitledBorder(null, "Avatar", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  panel.setBounds(25, 9, 181, 212);
  frmChiTiet.getContentPane().add(panel);
  panel.setLayout(null);

  path = new JTextField();
  path.setBounds(233, 201, 247, 20);
  frmChiTiet.getContentPane().add(path);
  path.setColumns(10);
  
  JLabel lblImages = new JLabel("");
  lblImages.setIcon(new ImageIcon("C:\\Users\\Lonely\\Desktop\\tv-smith-icon.png"));
  lblImages.setBounds(6, 16, 169, 189);
  panel.add(lblImages);
  
  JButton btnUpload = new JButton("Upload");
  btnUpload.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("images","jpg","png");
    fileChooser.setFileFilter(filter);
    
    int i = fileChooser.showOpenDialog(null);
    if(i == JFileChooser.APPROVE_OPTION){
     File  file = fileChooser.getSelectedFile();
      filename = file.getAbsolutePath();
     try {
      bi =  ImageIO.read(file);
      ImageIcon icon = new ImageIcon(bi);
      lblImages.setIcon(icon);
     } catch (Exception e2) {
      // TODO: handle exception
     }
    }
    
   }
  });
  btnUpload.setBounds(63, 242, 89, 23);
  frmChiTiet.getContentPane().add(btnUpload);

  JDateChooser txtDate = new JDateChooser();
  txtDate.setBounds(351, 118, 131, 20);
  frmChiTiet.getContentPane().add(txtDate);

  JButton btnInsert = new JButton("Insert");
  btnInsert.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    String sql = "INSERT INTO sinhvien.sinhvien (name, age, class, address, bỉthday,images) VALUES (?,?, ?, ?, ?, ?)";
    java.util.Date date = txtDate.getDate();
    String dateInput = new SimpleDateFormat("yyyy-MM-dd").format(date);
    
    //Images 
    FileInputStream fis;
    
    path.setText(filename);
    try {
      File imgfile = new File(filename);
      FileInputStream fin = new FileInputStream(imgfile);
     stmt = conn.prepareStatement(sql);
     stmt.setString(1, txtName.getText());
     stmt.setInt(2, Integer.parseInt(txtAge.getText()));
     stmt.setString(3, txtClass.getText());
     stmt.setString(4, txtAddress.getText());
     stmt.setString(5, dateInput);
     stmt.setBinaryStream(6, fin, (int) imgfile.length());

     stmt.executeUpdate();
     
    } catch (Exception e) {
     // TODO: handle exception
    }
   }
  });
  btnInsert.setBounds(338, 242, 89, 23);
  frmChiTiet.getContentPane().add(btnInsert);
  
  
  
  
 }
}



                    
                    
                    
                  

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang