Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class AdminProductAdd extends JFrame implements ActionListener {
- JLabel lblAdd_Customer, lblProduct_ID, lblProduct_Name, lblCompany_Name, lblProduct_Quantity, lblProduct_Price, lblProduct_Shelf, lblProduct_Category;
- JTextField txtProduct_ID, txtCompany_Name, txtProduct_Quantity, txtProduct_Price, txtProduct_Name;
- JComboBox comboShelf, comboCategory;
- ImageIcon iconAdd, iconReset, iconClose;
- JButton btnReset, btnAdd, btnClose;
- JPanel panelMain;
- JPanel panelNorth;
- Container container;
- AdminProductAdd() {
- container = getContentPane();
- container.setLayout(null);
- setTitle("Add Product");
- setSize(860, 700);
- //setExtendedState(JFrame.MAXIMIZED_BOTH);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setResizable(false);
- panelNorth = new JPanel();
- panelNorth.setLayout(null);
- panelNorth.setBackground(Color.yellow);
- panelNorth.setBounds(0, 0, 860, 100);
- add(panelNorth);
- lblAdd_Customer = new JLabel("ADD PRODUCT");
- lblAdd_Customer.setFont(new Font("Arial", Font.ITALIC, 30));
- lblAdd_Customer.setBorder(BorderFactory.createEtchedBorder());
- lblAdd_Customer.setBounds(300, 30, 260, 60);
- panelNorth.add(lblAdd_Customer);
- panelMain = new JPanel();
- panelMain.setBounds(100, 130, 650, 500);
- panelMain.setLayout(null);
- add(panelMain);
- lblProduct_ID = new JLabel("Product ID:");
- lblProduct_ID.setBounds(70, 50, 100, 16);
- panelMain.add(lblProduct_ID);
- lblProduct_Name = new JLabel("Product Name:");
- lblProduct_Name.setBounds(70, 110, 100, 16);
- panelMain.add(lblProduct_Name);
- lblCompany_Name = new JLabel("Company:");
- lblCompany_Name.setBounds(70, 170, 100, 16);
- panelMain.add(lblCompany_Name);
- lblProduct_Quantity = new JLabel("Quantity:");
- lblProduct_Quantity.setBounds(70, 230, 100, 16);
- panelMain.add(lblProduct_Quantity);
- lblProduct_Price = new JLabel("Price:");
- lblProduct_Price.setBounds(70, 300, 100, 16);
- panelMain.add(lblProduct_Price);
- txtProduct_ID = new JTextField();
- txtProduct_ID.setBounds(210, 50, 200, 20);
- panelMain.add(txtProduct_ID);
- txtProduct_Name = new JTextField();
- txtProduct_Name.setBounds(210, 110, 200, 20);
- panelMain.add(txtProduct_Name);
- txtCompany_Name = new JTextField();
- txtCompany_Name.setBounds(210, 170, 200, 20);
- panelMain.add(txtCompany_Name);
- txtProduct_Quantity = new JTextField();
- txtProduct_Quantity.setBounds(210, 230, 200, 20);
- panelMain.add(txtProduct_Quantity);
- txtProduct_Price = new JTextField();
- txtProduct_Price.setBounds(210, 300, 200, 20);
- panelMain.add(txtProduct_Price);
- String[] shelfNumber = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"};
- String[] category = {"Clouth", "Food", "Games"};
- lblProduct_Shelf = new JLabel("Shelf Number:");
- lblProduct_Category = new JLabel("Category:");
- comboCategory = new JComboBox(category);
- comboShelf = new JComboBox(shelfNumber);
- lblProduct_Shelf.setBounds(70, 360, 100, 20);
- comboShelf.setBounds(210, 360, 200, 20);
- lblProduct_Category.setBounds(70, 420, 100, 20);
- comboCategory.setBounds(210, 420, 200, 20);
- panelMain.add(lblProduct_Shelf);
- panelMain.add(lblProduct_Category);
- panelMain.add(comboCategory);
- panelMain.add(comboShelf);
- iconAdd = new ImageIcon("iconadd.png");
- iconReset = new ImageIcon("iconreset.png");
- iconClose = new ImageIcon("iconclose.png");
- btnAdd = new JButton("ADD", iconAdd);
- btnAdd.setBounds(440, 80, 153, 29);
- panelMain.add(btnAdd);
- btnReset = new JButton("RESET", iconReset);
- btnReset.setBounds(440, 225, 153, 29);
- panelMain.add(btnReset);
- btnClose = new JButton("COLSE", iconClose);
- btnClose.setBounds(440, 380, 153, 29);
- panelMain.add(btnClose);
- panelMain.setBorder(BorderFactory.createEtchedBorder());
- panelMain.setBackground(Color.yellow);
- setVisible(true);
- btnClose.addActionListener(this);
- btnReset.addActionListener(this);
- btnClose.addActionListener(this);
- btnReset.addActionListener(this);
- btnAdd.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- Connection con = myConnection.getConnection();
- PreparedStatement ps;
- if (ifEmpyty()) {
- try {
- ps = con.prepareStatement("INSERT INTO `product`(`product_id`, `product_name`, `product_company`, `product_quantity`, `product_price`, `product_shelf`, `product_category`) VALUES (?,?,?,?,?,?,?)");
- ps.setString(1, txtProduct_ID.getText());
- ps.setString(2, txtProduct_Name.getText());
- ps.setString(3, txtCompany_Name.getText());
- ps.setString(4, txtProduct_Quantity.getText());
- ps.setString(5, txtProduct_Price.getText());
- ps.setString(6, comboShelf.getSelectedItem().toString());
- ps.setString(7, comboCategory.getSelectedItem().toString());
- if (ifIDExit(txtProduct_ID.getText())) {
- JOptionPane.showMessageDialog(rootPane, "ID Already exits");
- }
- else{
- if (ps.executeUpdate() != 0) {
- JOptionPane.showMessageDialog(rootPane, "added");
- } else {
- JOptionPane.showMessageDialog(rootPane, "not added");
- }
- }
- } catch (SQLException ex) {
- Logger.getLogger(AdminProductAdd.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- });
- }
- public boolean ifEmpyty() {
- if (txtProduct_ID.getText().equals("") || txtCompany_Name.getText().equals("") || txtProduct_Quantity.getText().equals("")
- || txtProduct_Price.getText().equals("") || txtProduct_Name.getText().equals(""))
- {
- JOptionPane.showMessageDialog(null, "you didn't fill everyone");
- }
- return true;
- }
- public boolean ifIDExit(String un){
- boolean uExits=false;
- Connection con =myConnection.getConnection();
- PreparedStatement ps;
- ResultSet rs;
- try {
- ps=con.prepareStatement("SELECT * FROM `product` WHERE `product_id`=?");
- ps.setString(1, txtProduct_ID.getText());
- rs=ps.executeQuery();
- if (rs.next()) {
- uExits=true;
- }
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return uExits;
- }
- public static void main(String[] args) {
- new AdminProductAdd();
- }
- @Override
- public void actionPerformed(ActionEvent ae) {
- Object object = ae.getSource();
- if (object == btnClose) {
- System.exit(0);
- }
- if (object == btnReset) {
- txtCompany_Name.setText("");
- txtProduct_Name.setText("");
- txtProduct_ID.setText("");
- txtProduct_Price.setText("");
- txtProduct_Price.setText("");
- txtProduct_Quantity.setText("");
- comboShelf.setSelectedIndex(0);
- comboCategory.setSelectedIndex(0);
- }
- }
- }
- ==================================================================================================
- //class myConnection
- package project;
- import java.sql.*;
- public class myConnection {
- public static Connection getConnection() {
- Connection con = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost/uhd", "root", "");
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return con;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement