Created
May 16, 2020 02:11
-
-
Save lhpworking/77ac9b9dee251fb6f853c30a0071d92a to your computer and use it in GitHub Desktop.
Revisions
-
lhpworking created this gist
May 16, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,358 @@ package Forms; import Dbconection.prisonbase; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.io.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.time.LocalDateTime; import java.util.Calendar; import java.sql.Date; public class Prisoners extends JFrame { private JPanel defaultpanel; private JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9, lbl10, lbl11, lbl12, image, lbl13, lbl14, lbl15, lbl16, lbl17, lbl18; private JComboBox cbb7, cbb8, cbb9, cbb10, cbb11, cbbstatus; private JComboBox<Integer> dateboxA, monthboxA, yearboxA, dateboxB, monthboxB, yearboxB; private JTextField tf1, tf2, tf3; private JTextArea ta1; private String photo; private JButton btn1, btn2; private String status[] = { "Alive" }; private String gender[] = { "Select", "Male", "Female" }; private String dates[] = {"Date", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}; private String months[] = {"Month", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}; private String years[] = {"Year", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1981", "1982", "1983", "1984", "1986", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020"}; private String crimes[] = {"Select", "Cướp giật", "Sát nhân", "Sử dụng chất kích thích", "Mại dâm" }; private String punishments[] = {"Select", "Chung thân", "Tử Hình", "Giam 1 năm tù", "Giam 3 năm tù", "Giam 5 năm tù", "giam 10 năm tù" }; private String districts[] = {"Select", "Quận 1", "Quận 2", "Quận 3", "Quận 4", "Quận 5", "Quận 5", "Quận 6", "Quận 7", "Quận 8", "Quận 9", "Quận 10", "Quận 11", "Quận 12", "Quận Bình Tân", "Quận Bình thạnh", "Quận Gò Vấp", "Quận Phú Nhuận", "Quận Tân Bình", "Quận Tân phú ", "Quận Thủ Đức ", "Huyện Bình Chánh", "Huyện Cần Giờ ", "Huyện Nhà Bè", "Huyện Củ Chi", "Huyện Hóc Môn" }; private String[] cities = {"Select", "Tp Hồ Chí Minh", "Tp Hà Nội", "Tp Bà Rịa", "Tp Vũng Tàu", "Tp Đà Lạt", "Tp Đà Nẵng", "Tp Hải Phòng" }; public Prisoners(String title) throws HeadlessException { super(title); setLayout(null); defaultpanel = (JPanel) getContentPane(); prisonbase prisonbase=new prisonbase(); //init label lbl1 = new JLabel("Prisoner Id "); lbl1.setFont(new Font("Arial", Font.PLAIN, 20)); lbl1.setBounds(30, 10, 200, 50); defaultpanel.add(lbl1); lbl2 = new JLabel("Prisoner's Name "); lbl2.setBounds(30, 50, 200, 50); lbl2.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl2); lbl3 = new JLabel("Gender "); lbl3.setBounds(30, 90, 200, 50); lbl3.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl3); lbl4 = new JLabel("Phone Number "); lbl4.setBounds(30, 130, 200, 50); lbl4.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl4); lbl5 = new JLabel("Date Of Birth"); lbl5.setFont(new Font("Arial", Font.PLAIN, 20)); lbl5.setBounds(30, 170, 200, 50); defaultpanel.add(lbl5); lbl6 = new JLabel("Date Of Arrest "); lbl6.setBounds(30, 210, 200, 50); lbl6.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl6); lbl7 = new JLabel("Crime "); lbl7.setBounds(30, 250, 200, 50); lbl7.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl7); lbl8 = new JLabel("Punishment "); lbl8.setBounds(30, 290, 200, 50); lbl8.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl8); lbl9 = new JLabel("Address "); lbl9.setBounds(30, 330, 200, 50); lbl9.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl9); lbl10 = new JLabel("District "); lbl10.setBounds(30, 450, 200, 50); lbl10.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl10); lbl11 = new JLabel("City "); lbl11.setBounds(30, 490, 200, 50); lbl11.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl11); image = new JLabel(); image.setBounds(540, 70, 250, 250); image.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.lightGray)); defaultpanel.add(image); lbl12 = new JLabel("Status"); lbl12.setBounds(30, 530, 200, 50); lbl12.setFont(new Font("Arial", Font.PLAIN, 20)); defaultpanel.add(lbl12); lbl13 = new JLabel("Date:"); lbl13.setBounds(200, 175, 100, 40); lbl13.setFont(new Font("Arial", Font.PLAIN, 15)); defaultpanel.add(lbl13); lbl14 = new JLabel("Month:"); lbl14.setBounds(300, 175, 100, 40); lbl14.setFont(new Font("Arial", Font.PLAIN, 15)); defaultpanel.add(lbl14); lbl15 = new JLabel("Year:"); lbl15.setBounds(405, 175, 100, 40); lbl15.setFont(new Font("Arial", Font.PLAIN, 15)); defaultpanel.add(lbl15); lbl16 = new JLabel("Date:"); lbl16.setBounds(200, 215, 100, 40); lbl16.setFont(new Font("Arial", Font.PLAIN, 15)); defaultpanel.add(lbl16); lbl17 = new JLabel("Month:"); lbl17.setBounds(300, 215, 100, 40); lbl17.setFont(new Font("Arial", Font.PLAIN, 15)); defaultpanel.add(lbl17); lbl18 = new JLabel("Year:"); lbl18.setBounds(405, 215, 100, 40); lbl18.setFont(new Font("Arial", Font.PLAIN, 15)); defaultpanel.add(lbl18); //init textfield tf1 = new JTextField(); tf1.setBounds(200, 15, 300, 40); defaultpanel.add(tf1); tf2 = new JTextField(); tf2.setBounds(200, 55, 300, 40); defaultpanel.add(tf2); tf3 = new JTextField(); tf3.setBounds(200, 135, 300, 40); defaultpanel.add(tf3); //init textarea ta1 = new JTextArea(); ta1.setBounds(200, 335, 300, 120); defaultpanel.add(ta1); //combobox //set dữ liệu ngày tháng năm của DOB,DOA dateboxA = new JComboBox<>(); dateboxA.setBounds(235, 170, 60, 50); for (int i = 1; i <= 31; i++) { dateboxA.addItem(i); } defaultpanel.add(dateboxA); monthboxA = new JComboBox<>(); monthboxA.setBounds(345, 170, 60, 50); for (int i = 1; i <= 12; i++) { monthboxA.addItem(i); } defaultpanel.add(monthboxA); yearboxA = new JComboBox<>(); yearboxA.setBounds(440, 170, 90, 50); for (int i = 1940; i <= LocalDateTime.now().getYear(); i++) { yearboxA.addItem(i); } defaultpanel.add(yearboxA); dateboxB = new JComboBox<>(); dateboxB.setBounds(235, 210, 60, 50); for (int i = 1; i <= 31; i++) { dateboxB.addItem(i); } defaultpanel.add(dateboxB); monthboxB = new JComboBox<>(); monthboxB.setBounds(345, 210, 60, 50); for (int i = 1; i <= 12; i++) { monthboxB.addItem(i); } defaultpanel.add(monthboxB); yearboxB = new JComboBox<>(); yearboxB.setBounds(440, 210, 90, 50); for (int i = 1940; i <= LocalDateTime.now().getYear(); i++) { yearboxB.addItem(i); } defaultpanel.add(yearboxB); //================================================================\\ cbb7 = new JComboBox(crimes); cbb7.setBounds(200, 250, 300, 50); defaultpanel.add(cbb7); cbb8 = new JComboBox(punishments); cbb8.setBounds(200, 290, 300, 50); defaultpanel.add(cbb8); cbb9 = new JComboBox(districts); cbb9.setBounds(200, 450, 300, 50); defaultpanel.add(cbb9); cbb10 = new JComboBox(cities); cbb10.setBounds(200, 490, 300, 50); defaultpanel.add(cbb10); cbb11 = new JComboBox(gender); cbb11.setBounds(250, 90, 100, 50); defaultpanel.add(cbb11); cbbstatus = new JComboBox<String>(status); cbbstatus.setBounds(200, 530, 300, 50); defaultpanel.add(cbbstatus); //init button btn1 = new JButton("Save"); btn1.setBounds(250, 600, 200, 50); defaultpanel.add(btn1); btn2 = new JButton("Upload Image"); btn2.setBounds(570, 350, 200, 50); defaultpanel.add(btn2); // add actionlist btn2.addActionListener(e -> { //chọn file lấy dữ trong máy tính JFileChooser jFileChooser = new JFileChooser(); jFileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); FileNameExtensionFilter filter = new FileNameExtensionFilter("*.IMAGE", "jpg", "gif", "png"); jFileChooser.addChoosableFileFilter(filter); int result = jFileChooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File choosefile = jFileChooser.getSelectedFile(); String sourceimage = choosefile.getAbsolutePath(); //fit ảnh phù hợp với khung label "image" ImageIcon imageIcon = new ImageIcon(sourceimage); Image imagenew = imageIcon.getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH); image.setIcon(new ImageIcon(imagenew)); photo = sourceimage; //==================================================\\ } else if (result == JFileChooser.CANCEL_OPTION) { JOptionPane.showMessageDialog(btn2, "No Data"); } }); //==========================================================================\\ btn1.addActionListener(e -> { if (e.getSource() == btn1) { int i = 0; //lấy dữ ngày tháng năm đã chọn (12,09,2001) xếp thành -> 12-09-2001 (dd/mm/yyyy) Calendar Dob = Calendar.getInstance(); Dob.set(Calendar.DATE, (Integer) dateboxB.getSelectedItem()); Dob.set(Calendar.MONTH, (Integer) monthboxB.getSelectedItem()); Dob.set(Calendar.YEAR, (Integer) yearboxB.getSelectedItem()); Date DOB = new Date(Dob.getTimeInMillis()); Calendar Doa = Calendar.getInstance(); Doa.set(Calendar.DATE, (Integer) dateboxA.getSelectedItem()); Doa.set(Calendar.MONTH, (Integer) monthboxA.getSelectedItem()); Doa.set(Calendar.YEAR, (Integer) yearboxA.getSelectedItem()); Date DOA = new Date(Doa.getTimeInMillis()); //=================================================================\\ // thêm dữ liệu vào database try { Connection con = prisonbase.getDBconnection(); String sql = "INSERT INTO prisoners VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); InputStream inputStream = new FileInputStream(new File(photo)); ps.setString(1, tf1.getText()); ps.setString(2, tf2.getText()); ps.setString(3, (String) cbb11.getSelectedItem()); ps.setString(4, tf3.getText()); ps.setDate(5, DOB); ps.setDate(6, DOA); ps.setString(7, (String) cbb7.getSelectedItem()); ps.setString(8, (String) cbb8.getSelectedItem()); ps.setString(9, ta1.getText()); ps.setString(10, (String) cbb9.getSelectedItem()); ps.setString(11, (String) cbb10.getSelectedItem()); ps.setString(12, (String) cbbstatus.getSelectedItem()); ps.setBlob(13,inputStream); i = ps.executeUpdate(); if (i > 0) { JOptionPane.showMessageDialog(btn1, "Saved Sucesssfully"); dispose(); Prisoners prisoners = new Prisoners("Prisoner Detail"); } else { JOptionPane.showMessageDialog(btn1, "Failed"); } } catch (Exception exception) { exception.printStackTrace(); } } }); //===================================================================================\\ //=============================\\ setBounds(300, 50, 900, 700); setVisible(true); } }