27 April 2016

Upload images blob in mysql Java swing Eclipse Insert update delete search [Version 2]

Verson 2: Changes show images

DBConncetion.java
package DBConnection;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {

 // Driver and URL
 private static final String DRIVER_JDBC = "com.mysql.jdbc.Driver";
 private static final String URL_DB = "jdbc:mysql://localhost/sinhvien";
 // Pass and User
 private static final String USER = "root";
 private static final String PASS = "1234567";

 private static Connection conn;

 public static Connection getConnection() {
  try {
   Class.forName(DRIVER_JDBC);
   conn = DriverManager.getConnection(URL_DB, USER, PASS);
  } catch (Exception e) {
    System.out.println("Error connection "+e);
  }

  return conn;
 }

}
NOTE: Add liblary connect database mysql
Main.java

Show images in Main.java


package Main;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import DBConnection.DBConnection;
import ThongTin.ChiTiet;
import ThongTin.view;
import javax.swing.JTextField;

public class Main {

 public JFrame frmMain;

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

 ChiTiet windown = new ChiTiet();
 view vew = new view();
 private JTable table;
 private JTextField txtSearch;

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

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

 }

 public void showTable() {

  // Name Column table
  Vector cols = new Vector();
  cols.addElement("ID");
  cols.addElement("Name");
  cols.addElement("Age");
  cols.addElement("Class");
  cols.addElement("Address");
  cols.addElement("Birthday");
  cols.addElement("Images");

  // Data table
  Vector data = new Vector();
  String sql = "Select * From sinhvien.sinhvien";
  try {
   stmt = conn.prepareStatement(sql);
   rs = stmt.executeQuery();
   int row = 0;
   while (rs.next()) {
    Vector sinhvien = new Vector();
    sinhvien.addElement(rs.getString("id"));
    sinhvien.addElement(rs.getString("name"));
    sinhvien.addElement(rs.getInt("age"));
    sinhvien.addElement(rs.getString("class"));
    sinhvien.addElement(rs.getString("address"));
    sinhvien.addElement(rs.getDate("birthday"));
    sinhvien.addElement(rs.getBytes("images"));

    data.add(sinhvien);
   }

  } catch (Exception e) {
   // TODO: handle exception
  }
  table.setModel(new DefaultTableModel(data, cols));
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frmMain = new JFrame();
  frmMain.setTitle("Main");
  frmMain.setBounds(660, 200, 641, 424);
  frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frmMain.getContentPane().setLayout(null);

  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setBounds(10, 11, 605, 193);
  frmMain.getContentPane().add(scrollPane);

  JPanel panel_1 = new JPanel();
  panel_1.setBorder(new TitledBorder(null, "Button", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  panel_1.setBounds(10, 215, 605, 77);
  frmMain.getContentPane().add(panel_1);
  panel_1.setLayout(null);

  JButton btnChiTiet = new JButton("Detail");
  btnChiTiet.setBounds(28, 25, 101, 23);
  panel_1.add(btnChiTiet);
  btnChiTiet.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\SinhVien\\icon\\detail-icon.png"));

  JButton btnReset = new JButton("Reset");
  btnReset.setBounds(494, 25, 101, 23);
  panel_1.add(btnReset);
  btnReset.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\SinhVien\\icon\\Reload-icon (1).png"));

  JButton btnDelete = new JButton("Delete");
  btnDelete.setBounds(383, 25, 101, 23);
  panel_1.add(btnDelete);
  btnDelete.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\SinhVien\\icon\\Actions-edit-delete-icon.png"));

  JButton btnView = new JButton("View");
  btnView.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    vew.frmView.setVisible(true);
   }
  });
  btnView.setIcon(
    new ImageIcon("C:\\Users\\Lonely\\workspace\\SinhVien\\icon\\Programming-Show-Property-icon (1).png"));
  btnView.setBounds(158, 25, 101, 23);
  panel_1.add(btnView);

  JPanel panel_2 = new JPanel();
  panel_2.setBorder(new TitledBorder(null, "Chuc nang", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  panel_2.setBounds(10, 303, 605, 72);
  frmMain.getContentPane().add(panel_2);
  panel_2.setLayout(null);

  JComboBox comboBox = new JComboBox();
  comboBox.setModel(
    new DefaultComboBoxModel(new String[] {"All Search", "Name", "Age", "Class", "Address"}));
  comboBox.setBounds(26, 29, 103, 20);
  panel_2.add(comboBox);

  JButton btnSearch = new JButton("Search");
  btnSearch.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    Vector cols = new Vector();
    cols.addElement("ID");
    cols.addElement("Name");
    cols.addElement("Age");
    cols.addElement("Class");
    cols.addElement("Address");
    cols.addElement("Birthday");
    cols.addElement("Images");

    Vector data = new Vector();
    String Search = txtSearch.getText().trim();
    String Select = comboBox.getSelectedItem().toString();
    switch (Select) {
    case "All Search":
     String sql = "Select * From sinhvien.sinhvien";
     try {
      stmt = conn.prepareStatement(sql);
      rs = stmt.executeQuery();
      int row = 0;
      while (rs.next()) {
       Vector getData = new Vector();
       getData.addElement(rs.getString("id"));
       getData.addElement(rs.getString("name"));
       getData.addElement(rs.getInt("age"));
       getData.addElement(rs.getString("class"));
       getData.addElement(rs.getString("address"));
       getData.addElement(rs.getDate("birthday"));
       getData.addElement(rs.getBytes("images"));

       if (getData.toString().contains(Search)) {

        data.addElement(getData);
       }
      }

     } catch (Exception e1) {
      // TODO: handle exception
     }
     break;
    case "Name":
     try {
      String sqlName = "Select * from sinhvien.sinhvien Where name LIKE ?";
      stmt = conn.prepareStatement(sqlName);
      stmt.setString(1, txtSearch.getText() + "%");
      rs = stmt.executeQuery();
      while (rs.next()) {
       Vector getData = new Vector();
       getData.addElement(rs.getString("id"));
       getData.addElement(rs.getString("name"));
       getData.addElement(rs.getInt("age"));
       getData.addElement(rs.getString("class"));
       getData.addElement(rs.getString("address"));
       getData.addElement(rs.getDate("birthday"));
       getData.addElement(rs.getBytes("images"));

       data.addElement(getData);
      }

     } catch (Exception e2) {
      // TODO: handle exception
     }
     break;
    case "Class":
     try {
      String sqlName = "Select * from sinhvien.sinhvien Where class LIKE ?";
      stmt = conn.prepareStatement(sqlName);
      stmt.setString(1, "%"+txtSearch.getText() + "%");
      rs = stmt.executeQuery();
      while (rs.next()) {
       Vector getData = new Vector();
       getData.addElement(rs.getString("id"));
       getData.addElement(rs.getString("name"));
       getData.addElement(rs.getInt("age"));
       getData.addElement(rs.getString("class"));
       getData.addElement(rs.getString("address"));
       getData.addElement(rs.getDate("birthday"));
       getData.addElement(rs.getBytes("images"));

       data.addElement(getData);
      }

     } catch (Exception e2) {
      // TODO: handle exception
     }
     break;
    case "Age":
     try {
      String sqlName = "Select * from sinhvien.sinhvien Where age LIKE ?";
      stmt = conn.prepareStatement(sqlName);
      stmt.setInt(1, Integer.parseInt(txtSearch.getText()));
      rs = stmt.executeQuery();
      while (rs.next()) {
       Vector getData = new Vector();
       getData.addElement(rs.getString("id"));
       getData.addElement(rs.getString("name"));
       getData.addElement(rs.getInt("age"));
       getData.addElement(rs.getString("class"));
       getData.addElement(rs.getString("address"));
       getData.addElement(rs.getDate("birthday"));
       getData.addElement(rs.getBytes("images"));

       data.addElement(getData);
      }

     } catch (Exception e2) {
      // TODO: handle exception
     }
     break;
    case "Address":
     try {
      String sqlName = "Select * from sinhvien.sinhvien Where address LIKE ?";
      stmt = conn.prepareStatement(sqlName);
      stmt.setString(1, txtSearch.getText() + "%");
      rs = stmt.executeQuery();
      while (rs.next()) {
       Vector getData = new Vector();
       getData.addElement(rs.getString("id"));
       getData.addElement(rs.getString("name"));
       getData.addElement(rs.getInt("age"));
       getData.addElement(rs.getString("class"));
       getData.addElement(rs.getString("address"));
       getData.addElement(rs.getDate("birthday"));
       getData.addElement(rs.getBytes("images"));

       data.addElement(getData);
      }

     } catch (Exception e2) {
      // TODO: handle exception
     }
     break;

    }
    table.setModel(new DefaultTableModel(data, cols));
   }
}); btnSearch.setBounds(322, 28, 89, 23); panel_2.add(btnSearch); table = new JTable(); table.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { int row = table.getSelectedRow(); windown.txtID.setText(table.getValueAt(row, 0).toString()); windown.txtName.setText(table.getValueAt(row, 1).toString()); windown.txtAge.setText(table.getValueAt(row, 2).toString()); windown.txtClass.setText(table.getValueAt(row, 3).toString()); windown.txtAddress.setText(table.getValueAt(row, 4).toString()); windown.txtBirthday.setDate((Date) table.getValueAt(row, 5)); vew.txtID.setText(table.getValueAt(row, 0).toString()); vew.txtName.setText(table.getValueAt(row, 1).toString()); vew.txtAge.setText(table.getValueAt(row, 2).toString()); vew.txtClass.setText(table.getValueAt(row, 3).toString()); vew.txtAddress.setText(table.getValueAt(row, 4).toString()); vew.txtBirthday.setText(table.getValueAt(row, 5).toString()); //convert object to byte[] java // ByteArrayOutputStream bos = new ByteArrayOutputStream(); // ObjectOutputStream oos; // try { // oos = new ObjectOutputStream(bos); // oos.writeObject(table.getValueAt(row, 6)); // oos.flush(); // // byte[] imagedata = bos.toByteArray(); // ImageIcon format = new ImageIcon(imagedata); // // vew.lblImages.setIcon(format); // // oos.close(); // bos.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } int id = Integer.parseInt(vew.txtID.getText()); String sql = "Select * From sinhvien.sinhvien where id =?"; try { stmt = conn.prepareStatement(sql); stmt.setInt(1, id); rs = stmt.executeQuery(); while (rs.next()) { byte[] imagedata = rs.getBytes("images"); ImageIcon format = new ImageIcon(imagedata); vew.lblImages.setIcon(format); } } catch (Exception e) { // TODO: handle exception } } }); scrollPane.setViewportView(table); txtSearch = new JTextField(); txtSearch.setBounds(156, 29, 132, 20); panel_2.add(txtSearch); txtSearch.setColumns(10); btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { int id = Integer.parseInt(windown.txtID.getText()); String sql = "Delete From sinhvien.sinhvien Where id =?"; try { stmt = conn.prepareStatement(sql); stmt.setInt(1, id); int reponse = JOptionPane.showConfirmDialog(frmMain, "Do you want delete?", "Delete person", JOptionPane.YES_NO_OPTION); String message = "?"; switch (reponse) { case JOptionPane.YES_OPTION: message = "YES_OPTION"; stmt.executeUpdate(); break; case JOptionPane.NO_OPTION: message = "NO_OPTION"; break; case JOptionPane.CLOSED_OPTION: message = "CLOSED_OPTION"; break; default: message = "autre"; } JOptionPane.showMessageDialog(frmMain, "response: " + message); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Ban chua chon ID"); } showTable(); } }); btnReset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { showTable(); } }); btnChiTiet.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { windown.frmChiTiet.setVisible(true); // frmMain.setVisible(false); } }); } }
View.java

package ThongTin;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;

import DBConnection.DBConnection;
import Main.Main;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class View {

 public JFrame frmView;

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

 public JLabel txtID;
 public JLabel txtName;
 public JLabel txtClass;
 public JLabel txtAddress;
 public JLabel txtBirthday;
 public JLabel txtAge;
 public JLabel lblImages;

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

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

 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frmView = new JFrame();
  frmView.setTitle("View");
  frmView.setBounds(50, 380, 536, 338);
  frmView.getContentPane().setLayout(null);

  JLabel lblName = new JLabel("Name:");
  lblName.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblName.setBounds(219, 113, 46, 14);
  frmView.getContentPane().add(lblName);

  txtName = new JLabel("");
  txtName.setBounds(295, 113, 151, 14);
  frmView.getContentPane().add(txtName);

  JLabel lblId = new JLabel("ID:");
  lblId.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblId.setBounds(219, 84, 46, 14);
  frmView.getContentPane().add(lblId);

  txtID = new JLabel("");
  txtID.setBounds(294, 84, 152, 14);
  frmView.getContentPane().add(txtID);

  txtBirthday = new JLabel("");
  txtBirthday.setBounds(295, 142, 151, 14);
  frmView.getContentPane().add(txtBirthday);

  txtClass = new JLabel("");
  txtClass.setBounds(295, 167, 151, 14);
  frmView.getContentPane().add(txtClass);

  txtAddress = new JLabel("");
  txtAddress.setBounds(294, 192, 151, 14);
  frmView.getContentPane().add(txtAddress);

  JLabel lblClass = new JLabel("Birthday:");
  lblClass.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblClass.setBounds(219, 142, 78, 14);
  frmView.getContentPane().add(lblClass);

  JLabel lblAddress = new JLabel("Class:");
  lblAddress.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblAddress.setBounds(219, 167, 78, 14);
  frmView.getContentPane().add(lblAddress);

  JLabel lblAddress_1 = new JLabel("Address:");
  lblAddress_1.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblAddress_1.setBounds(219, 192, 78, 14);
  frmView.getContentPane().add(lblAddress_1);

  JPanel panel_1 = new JPanel();
  panel_1.setBorder(
    new TitledBorder(null, "Thong tin sinh vien", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  panel_1.setBounds(175, 58, 335, 198);
  frmView.getContentPane().add(panel_1);
  panel_1.setLayout(null);

  JLabel lblAges = new JLabel("Age:");
  lblAges.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblAges.setBounds(45, 162, 78, 14);
  panel_1.add(lblAges);

  txtAge = new JLabel("");
  txtAge.setBounds(119, 162, 151, 14);
  panel_1.add(txtAge);

  JPanel panel_2 = new JPanel();
  panel_2.setBorder(new TitledBorder(null, "Avatar", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  panel_2.setBounds(10, 58, 155, 198);
  frmView.getContentPane().add(panel_2);
  panel_2.setLayout(null);

  lblImages = new JLabel("");
  lblImages.setBounds(10, 21, 135, 166);
  panel_2.add(lblImages);

  JLabel lblThongTinSinh = new JLabel("Thong Tin Sinh Vien Aptech FPT");
  lblThongTinSinh.setFont(new Font("Castellar", Font.BOLD, 19));
  lblThongTinSinh.setBounds(60, 11, 423, 36);
  frmView.getContentPane().add(lblThongTinSinh);

  JButton btnShow = new JButton("show");
  btnShow.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    show();
   }
  });
  btnShow.setBounds(50, 267, 89, 23);
  frmView.getContentPane().add(btnShow);
 }

 public void show() {
  // Data table
  int id = Integer.parseInt(txtID.getText());
  String sql = "Select * From sinhvien.sinhvien where id =?";
  try {
   stmt = conn.prepareStatement(sql);
   stmt.setInt(1, id);
   rs = stmt.executeQuery();
   while (rs.next()) {

    byte[] imagedata = rs.getBytes("images");
    ImageIcon format = new ImageIcon(imagedata);
    lblImages.setIcon(format);

   }

  } catch (Exception e) {
   // TODO: handle exception
  }

 }

}
Detail.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.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
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;
import Main.Main;

public class Detail {

 public JFrame frmChiTiet;
 public JTextField txtName;
 public JTextField txtAge;
 public JTextField txtClass;
 public JTextField txtAddress;
 private JTextField path;
 public JTextField txtID;
 public JDateChooser txtBirthday;

 String filename = null;
 BufferedImage bi;

 java.util.Date date;
 String dateInput;

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

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

 /**
  * Initialize the contents of the frame.
  */

 private void initialize() {
  frmChiTiet = new JFrame();
  frmChiTiet.setTitle("Detail");
  frmChiTiet.setBounds(50, 10, 517, 360);
  frmChiTiet.getContentPane().setLayout(null);

  JPanel panel = new JPanel();
  panel.setBorder(new TitledBorder(null, "Avatar", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  panel.setBounds(10, 9, 194, 246);
  frmChiTiet.getContentPane().add(panel);
  panel.setLayout(null);

  path = new JTextField();
  path.setBounds(140, 278, 238, 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(22, 24, 149, 198);
  panel.add(lblImages);

  JButton btnUpload = new JButton("Brown");
  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(25, 277, 89, 23);
  frmChiTiet.getContentPane().add(btnUpload);

  JPanel panel_2 = new JPanel();
  panel_2.setBorder(new TitledBorder(null, "Input", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  panel_2.setBounds(204, 9, 287, 246);
  frmChiTiet.getContentPane().add(panel_2);
  panel_2.setLayout(null);

  JButton btnInsert = new JButton("Insert");
  btnInsert.setBounds(168, 201, 100, 34);
  panel_2.add(btnInsert);
  btnInsert.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\SinhVien\\icon\\add-icon.png"));

  txtAddress = new JTextField();
  txtAddress.setBounds(139, 170, 129, 20);
  panel_2.add(txtAddress);
  txtAddress.setColumns(10);

  txtName = new JTextField();
  txtName.setBounds(139, 46, 129, 20);
  panel_2.add(txtName);
  txtName.setColumns(10);

  txtBirthday = new JDateChooser();
  txtBirthday.setBounds(139, 139, 131, 20);
  panel_2.add(txtBirthday);

  txtClass = new JTextField();
  txtClass.setBounds(139, 108, 129, 20);
  panel_2.add(txtClass);
  txtClass.setColumns(10);

  txtAge = new JTextField();
  txtAge.setBounds(139, 77, 129, 20);
  panel_2.add(txtAge);
  txtAge.setColumns(10);

  txtID = new JTextField();
  txtID.setBounds(139, 15, 129, 20);
  panel_2.add(txtID);
  txtID.setColumns(10);

  JLabel lblAddress = new JLabel("Address");
  lblAddress.setBounds(29, 176, 75, 14);
  panel_2.add(lblAddress);

  JLabel lblNgaySinh = new JLabel("Ngay sinh");
  lblNgaySinh.setBounds(29, 147, 75, 14);
  panel_2.add(lblNgaySinh);

  JLabel lblLop = new JLabel("Lop");
  lblLop.setBounds(29, 113, 75, 14);
  panel_2.add(lblLop);

  JLabel lblTuoi = new JLabel("Tuoi");
  lblTuoi.setBounds(29, 82, 75, 14);
  panel_2.add(lblTuoi);

  JLabel lblTen = new JLabel("Ten");
  lblTen.setBounds(29, 51, 75, 14);
  panel_2.add(lblTen);

  JLabel lblId = new JLabel("ID");
  lblId.setBounds(29, 18, 46, 14);
  panel_2.add(lblId);

  JButton btnUpdate = new JButton("Update");
  btnUpdate.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    int id = Integer.parseInt(txtID.getText());
    String sql = "UPDATE sinhvien.sinhvien SET name=?, age=?, class=?, address=?,birthday=?, images=? WHERE id=?";

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

    path.setText(filename);
    try {
     // DATE
     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.setInt(7, id);
     stmt.executeUpdate();
     System.out.println("Update OK");

    } catch (Exception e) {
     // TODO: handle exception
     System.out.println("Loi Update " + e);
    }

   }
  });
  btnUpdate.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\SinhVien\\icon\\Actions-edit-redo-icon.png"));
  btnUpdate.setBounds(29, 201, 108, 34);
  panel_2.add(btnUpdate);

  JButton btnReset = new JButton("Reset");
  btnReset.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    txtID.setText("");
    txtName.setText("");
    txtClass.setText("");
    txtAddress.setText("");
    txtAge.setText("");
    lblImages.setText("");
    path.setText("");
    txtBirthday.setDate(date);

   }
  });
  btnReset.setBounds(402, 277, 89, 23);
  frmChiTiet.getContentPane().add(btnReset);

  btnInsert.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    String sql = "INSERT INTO sinhvien.sinhvien (name, age, class, address, birthday, images) VALUES (?,?, ?, ?, ?, ?)";
    date = txtBirthday.getDate();
    dateInput = new SimpleDateFormat("yyyy-MM-dd").format(date);
    // Images
    FileInputStream fis;

    path.setText(filename);
    try {
     // DATE
     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();
     System.out.println("Insert ok");

    } catch (Exception e) {
     // TODO: handle exception
     System.out.println("Loi Insert: " + e);
    }
   }
  });

 }
}

Download import: https://drive.google.com/open?id=0BzClcKmnT6Ybc3RMdTFxSm93dlk
Back version 1 >> http://giai-ma.blogspot.com/2016/04/create-database-mysql-workbench.html

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang