Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. package project;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.sql.*;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. public class AdminProductAdd extends JFrame implements ActionListener {
  11.    
  12.     JLabel lblAdd_Customer, lblProduct_ID, lblProduct_Name, lblCompany_Name, lblProduct_Quantity, lblProduct_Price, lblProduct_Shelf, lblProduct_Category;
  13.     JTextField txtProduct_ID, txtCompany_Name, txtProduct_Quantity, txtProduct_Price, txtProduct_Name;
  14.     JComboBox comboShelf, comboCategory;
  15.     ImageIcon iconAdd, iconReset, iconClose;
  16.     JButton btnReset, btnAdd, btnClose;
  17.    
  18.     JPanel panelMain;
  19.     JPanel panelNorth;
  20.     Container container;
  21.    
  22.     AdminProductAdd() {
  23.         container = getContentPane();
  24.         container.setLayout(null);
  25.         setTitle("Add Product");
  26.         setSize(860, 700);
  27. //setExtendedState(JFrame.MAXIMIZED_BOTH);  
  28.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  29.         setLocationRelativeTo(null);
  30.         setResizable(false);
  31.        
  32.         panelNorth = new JPanel();
  33.         panelNorth.setLayout(null);
  34.         panelNorth.setBackground(Color.yellow);
  35.         panelNorth.setBounds(0, 0, 860, 100);
  36.         add(panelNorth);
  37.        
  38.         lblAdd_Customer = new JLabel("ADD PRODUCT");
  39.         lblAdd_Customer.setFont(new Font("Arial", Font.ITALIC, 30));
  40.         lblAdd_Customer.setBorder(BorderFactory.createEtchedBorder());
  41.         lblAdd_Customer.setBounds(300, 30, 260, 60);
  42.         panelNorth.add(lblAdd_Customer);
  43.        
  44.         panelMain = new JPanel();
  45.         panelMain.setBounds(100, 130, 650, 500);
  46.        
  47.         panelMain.setLayout(null);
  48.         add(panelMain);
  49.        
  50.         lblProduct_ID = new JLabel("Product ID:");
  51.         lblProduct_ID.setBounds(70, 50, 100, 16);
  52.         panelMain.add(lblProduct_ID);
  53.        
  54.         lblProduct_Name = new JLabel("Product Name:");
  55.         lblProduct_Name.setBounds(70, 110, 100, 16);
  56.         panelMain.add(lblProduct_Name);
  57.        
  58.         lblCompany_Name = new JLabel("Company:");
  59.         lblCompany_Name.setBounds(70, 170, 100, 16);
  60.         panelMain.add(lblCompany_Name);
  61.        
  62.         lblProduct_Quantity = new JLabel("Quantity:");
  63.         lblProduct_Quantity.setBounds(70, 230, 100, 16);
  64.         panelMain.add(lblProduct_Quantity);
  65.        
  66.         lblProduct_Price = new JLabel("Price:");
  67.         lblProduct_Price.setBounds(70, 300, 100, 16);
  68.         panelMain.add(lblProduct_Price);
  69.        
  70.         txtProduct_ID = new JTextField();
  71.         txtProduct_ID.setBounds(210, 50, 200, 20);
  72.         panelMain.add(txtProduct_ID);
  73.        
  74.         txtProduct_Name = new JTextField();
  75.         txtProduct_Name.setBounds(210, 110, 200, 20);
  76.         panelMain.add(txtProduct_Name);
  77.        
  78.         txtCompany_Name = new JTextField();
  79.         txtCompany_Name.setBounds(210, 170, 200, 20);
  80.         panelMain.add(txtCompany_Name);
  81.        
  82.         txtProduct_Quantity = new JTextField();
  83.         txtProduct_Quantity.setBounds(210, 230, 200, 20);
  84.         panelMain.add(txtProduct_Quantity);
  85.        
  86.         txtProduct_Price = new JTextField();
  87.         txtProduct_Price.setBounds(210, 300, 200, 20);
  88.         panelMain.add(txtProduct_Price);
  89.        
  90.         String[] shelfNumber = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"};
  91.         String[] category = {"Clouth", "Food", "Games"};
  92.        
  93.         lblProduct_Shelf = new JLabel("Shelf Number:");
  94.         lblProduct_Category = new JLabel("Category:");
  95.         comboCategory = new JComboBox(category);
  96.         comboShelf = new JComboBox(shelfNumber);
  97.        
  98.         lblProduct_Shelf.setBounds(70, 360, 100, 20);
  99.         comboShelf.setBounds(210, 360, 200, 20);
  100.        
  101.         lblProduct_Category.setBounds(70, 420, 100, 20);
  102.         comboCategory.setBounds(210, 420, 200, 20);
  103.        
  104.         panelMain.add(lblProduct_Shelf);
  105.         panelMain.add(lblProduct_Category);
  106.         panelMain.add(comboCategory);
  107.         panelMain.add(comboShelf);
  108.        
  109.         iconAdd = new ImageIcon("iconadd.png");
  110.         iconReset = new ImageIcon("iconreset.png");
  111.         iconClose = new ImageIcon("iconclose.png");
  112.        
  113.         btnAdd = new JButton("ADD", iconAdd);
  114.         btnAdd.setBounds(440, 80, 153, 29);
  115.         panelMain.add(btnAdd);
  116.        
  117.         btnReset = new JButton("RESET", iconReset);
  118.         btnReset.setBounds(440, 225, 153, 29);
  119.         panelMain.add(btnReset);
  120.        
  121.         btnClose = new JButton("COLSE", iconClose);
  122.         btnClose.setBounds(440, 380, 153, 29);
  123.         panelMain.add(btnClose);
  124.         panelMain.setBorder(BorderFactory.createEtchedBorder());
  125.         panelMain.setBackground(Color.yellow);
  126.         setVisible(true);
  127.    
  128.         btnClose.addActionListener(this);
  129.         btnReset.addActionListener(this);
  130.        
  131.         btnClose.addActionListener(this);
  132.         btnReset.addActionListener(this);
  133.         btnAdd.addActionListener(new ActionListener() {
  134.             @Override
  135.             public void actionPerformed(ActionEvent ae) {
  136.                 Connection con = myConnection.getConnection();
  137.                 PreparedStatement ps;
  138.                
  139.                 if (ifEmpyty()) {
  140.                      try {
  141.                     ps = con.prepareStatement("INSERT INTO `product`(`product_id`, `product_name`, `product_company`, `product_quantity`, `product_price`, `product_shelf`, `product_category`) VALUES (?,?,?,?,?,?,?)");
  142.                     ps.setString(1, txtProduct_ID.getText());
  143.                     ps.setString(2, txtProduct_Name.getText());
  144.                     ps.setString(3, txtCompany_Name.getText());
  145.                     ps.setString(4, txtProduct_Quantity.getText());
  146.                     ps.setString(5, txtProduct_Price.getText());
  147.                     ps.setString(6, comboShelf.getSelectedItem().toString());
  148.                     ps.setString(7, comboCategory.getSelectedItem().toString());
  149.  
  150.                    
  151.                          if (ifIDExit(txtProduct_ID.getText())) {
  152.                               JOptionPane.showMessageDialog(rootPane, "ID Already exits");
  153.                          }
  154.                          else{
  155.                               if (ps.executeUpdate() != 0) {
  156.                         JOptionPane.showMessageDialog(rootPane, "added");
  157.                     } else {
  158.                         JOptionPane.showMessageDialog(rootPane, "not added");
  159.                     }
  160.                              
  161.                          }
  162.                    
  163.                    
  164.  
  165.                 } catch (SQLException ex) {
  166.                     Logger.getLogger(AdminProductAdd.class.getName()).log(Level.SEVERE, null, ex);
  167.  
  168.                 }
  169.                 }
  170.                
  171.  
  172.             }
  173.         });
  174.     }
  175.  
  176.     public boolean ifEmpyty() {
  177.        
  178.         if (txtProduct_ID.getText().equals("") || txtCompany_Name.getText().equals("") || txtProduct_Quantity.getText().equals("")
  179.                 || txtProduct_Price.getText().equals("") || txtProduct_Name.getText().equals(""))
  180.         {
  181.             JOptionPane.showMessageDialog(null, "you didn't fill everyone");
  182.         }
  183.         return true;
  184.  
  185.     }
  186.     public boolean ifIDExit(String un){
  187.        boolean uExits=false;
  188.         Connection con =myConnection.getConnection();
  189.         PreparedStatement ps;
  190.         ResultSet rs;
  191.         try {
  192.             ps=con.prepareStatement("SELECT * FROM `product` WHERE `product_id`=?");
  193.             ps.setString(1, txtProduct_ID.getText());
  194.            
  195.             rs=ps.executeQuery();
  196.            
  197.             if (rs.next()) {
  198.                 uExits=true;
  199.                
  200.             }
  201.         } catch (Exception e) {
  202.             System.out.println(e.getMessage());
  203.         }
  204.        
  205.         return uExits;
  206.     }
  207.     public static void main(String[] args) {
  208.         new AdminProductAdd();
  209.     }
  210.  
  211.     @Override
  212.         public void actionPerformed(ActionEvent ae) {
  213.         Object object = ae.getSource();
  214.  
  215.         if (object == btnClose) {
  216.             System.exit(0);
  217.         }
  218.         if (object == btnReset) {
  219.  
  220.             txtCompany_Name.setText("");
  221.             txtProduct_Name.setText("");
  222.             txtProduct_ID.setText("");
  223.             txtProduct_Price.setText("");
  224.             txtProduct_Price.setText("");
  225.             txtProduct_Quantity.setText("");
  226.             comboShelf.setSelectedIndex(0);
  227.             comboCategory.setSelectedIndex(0);
  228.  
  229.         }
  230.     }
  231. }
  232. ==================================================================================================
  233. //class myConnection
  234. package project;
  235.  
  236. import java.sql.*;
  237.  
  238. public class myConnection {
  239.  
  240.     public static Connection getConnection() {
  241.         Connection con = null;
  242.  
  243.         try {
  244.  
  245.             Class.forName("com.mysql.jdbc.Driver");
  246.             con = DriverManager.getConnection("jdbc:mysql://localhost/uhd", "root", "");
  247.  
  248.         } catch (Exception e) {
  249.             System.out.println(e.getMessage());
  250.         }
  251.  
  252.         return con;
  253.  
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement