19 April 2016

Swing Login Manager Java [GUI] Database Insert Update Delete Search , Eclipse

Phần mềm demo đăng nhập vào hệ thống quản lý bằng Java swing Gui


Login successfully


About


DBConnection.java << mysql workbench
package Connect;

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

public class DBConnection {

 private static Connection conn;
 private static String DRIVER = "com.mysql.jdbc.Driver";
 private static String URL = "jdbc:mysql://localhost:3306/manager";
 private static String USER = "root";
 private static String PASS = "1234567";
 
 public static Connection getConnection(){
  try {
   Class.forName(DRIVER);
   conn = DriverManager.getConnection(URL,USER,PASS);
  } catch (Exception e) {
   System.out.println(e);
  }
  return conn;
 }
}

NOTE: Add liblary

Login.java
package Login;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Admin.Manager_Login;
import Connect.DBConnection;

public class Login {

 private JFrame frame;
 private JTextField txtUser;

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

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

 /**
  * Create the application.
  */
 public Login() {
  initializeLogin();
  try {
   conn = DBConnection.getConnection();
  } catch (Exception e) {
   // TODO: handle exception
  }
  this.frame.setResizable(false);
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initializeLogin() {
  frame = new JFrame();
  frame.setBounds(100, 100, 387, 305);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);

  JLabel lblLogin = new JLabel("Login");
  lblLogin.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\user-login-icon.png"));
  lblLogin.setFont(new Font("Simplified Arabic Fixed", Font.BOLD, 37));
  lblLogin.setBounds(93, 11, 207, 66);
  frame.getContentPane().add(lblLogin);

  txtUser = new JTextField();
  txtUser.setBounds(162, 103, 133, 20);
  frame.getContentPane().add(txtUser);
  txtUser.setColumns(10);

  JButton btnLogin = new JButton("Login");
  btnLogin.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\About-me-icon.png"));
  btnLogin.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String User = txtUser.getText();
    String Pass = txtPass.getText();

    String sql = "SELECT * FROM manager.login WHERE user=? and pass=?";
    try {
     stmt = conn.prepareStatement(sql);
     stmt.setString(1, User);
     stmt.setString(2, Pass);

     rs = stmt.executeQuery();
     if (rs.next()) {

      String user = rs.getString("roles");
      switch (user) {
      case "Admin":
       Manager_Login window = new Manager_Login();
       frame.dispose();
       window.frame.setVisible(true);

       break;
      case "Saler":
       JOptionPane.showMessageDialog(null, "Xin loi ban la Saler khong phai admin");
       break;
      case "Acountant":
       JOptionPane.showMessageDialog(null, "Xin loi ban la Acountant khong phai admin");
       break;
      case "Manager":
       JOptionPane.showMessageDialog(null, "Xin loi ban la Manager khong phai admin");
       break;
      default:
       break;
      }
     } else {
      JOptionPane.showMessageDialog(null, "User or Pass khong dung!");
     }

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

  btnLogin.setBounds(75, 186, 89, 23);
  frame.getContentPane().add(btnLogin);

  JButton btnExit = new JButton("Exit");
  btnExit.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\Actions-window-close-icon.png"));
  btnExit.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });
  btnExit.setBounds(207, 186, 89, 23);
  frame.getContentPane().add(btnExit);

  JLabel lblUsername = new JLabel("Username:");
  lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblUsername.setBounds(60, 106, 74, 14);
  frame.getContentPane().add(lblUsername);

  JLabel lblPassword = new JLabel("Password:");
  lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
  lblPassword.setBounds(60, 147, 74, 14);
  frame.getContentPane().add(lblPassword);

  txtPass = new JPasswordField();
  txtPass.setBounds(162, 144, 133, 20);
  frame.getContentPane().add(txtPass);
 }
}

Manager_Login.java
package Admin;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import Connect.DBConnection;

public class Manager_Login {

 public JFrame frame;
 private JTextField txtID;
 private JTextField txtUsername;
 private JTable table;
 private JPasswordField txtPassword;

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

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

 /**
  * Create the application.
  */

 public Manager_Login() {
  initialize();
  try {
   conn = DBConnection.getConnection();
  } catch (Exception e) {
   // TODO: handle exception
  }
  showTable();
  this.frame.setResizable(false);
 }

 public void showTable() {
  Vector cols = new Vector();
  cols.addElement("ID");
  cols.addElement("Username");
  cols.addElement("Password");
  cols.addElement("Roles");

  // Data
  Vector data = new Vector();
  String sql = "SELECT * FROM manager.login";
  try {
   stmt = conn.prepareStatement(sql);
   rs = stmt.executeQuery();
   while (rs.next()) {
    Vector login = new Vector();
    login.addElement(rs.getString("id"));
    login.addElement(rs.getString("user"));
    login.addElement(rs.getString("pass"));
    login.addElement(rs.getString("roles"));

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

 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {

  frame = new JFrame();
  frame.setBounds(100, 100, 404, 428);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);

  txtID = new JTextField();
  txtID.setBounds(123, 54, 86, 20);
  frame.getContentPane().add(txtID);
  txtID.setColumns(10);

  txtUsername = new JTextField();
  txtUsername.setBounds(123, 85, 86, 20);
  frame.getContentPane().add(txtUsername);
  txtUsername.setColumns(10);

  txtPassword = new JPasswordField();
  txtPassword.setBounds(123, 112, 86, 20);
  frame.getContentPane().add(txtPassword);

  JComboBox comboBox = new JComboBox();
  comboBox.setModel(new DefaultComboBoxModel(new String[] { "Admin", "Acountant", "Manager", "Saler" }));
  comboBox.setBounds(123, 143, 86, 20);
  frame.getContentPane().add(comboBox);

  JLabel lblId = new JLabel("ID:");
  lblId.setBounds(36, 57, 61, 14);
  frame.getContentPane().add(lblId);

  JLabel lblUsername = new JLabel("Username:");
  lblUsername.setBounds(36, 88, 77, 14);
  frame.getContentPane().add(lblUsername);

  JLabel lblPassword = new JLabel("Password:");
  lblPassword.setBounds(36, 115, 77, 14);
  frame.getContentPane().add(lblPassword);

  JLabel lblRoles = new JLabel("Roles:");
  lblRoles.setBounds(36, 146, 61, 14);
  frame.getContentPane().add(lblRoles);

  JButton btnAdd = new JButton("Add");
  btnAdd.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    String sql = "INSERT INTO manager.login (id,user, pass, roles) VALUES (?,?,?,?)";
    try {
     stmt = conn.prepareStatement(sql);
     stmt.setInt(1, Integer.parseInt(txtID.getText()));
     stmt.setString(2, txtUsername.getText());
     stmt.setString(3, txtPassword.getText());
     stmt.setString(4, comboBox.getSelectedItem().toString());

     stmt.executeUpdate();

    } catch (Exception e) {
     // TODO: handle exception
    }
    showTable();
   }
  });
  btnAdd.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\add-icon.png"));
  btnAdd.setBounds(234, 53, 106, 23);
  frame.getContentPane().add(btnAdd);

  JButton btnUpdate = new JButton("Update");
  btnUpdate.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String sql = "UPDATE manager.login SET user=?, pass=?, roles=? WHERE id =?";
    int id = Integer.parseInt(txtID.getText());
    try {
     stmt = conn.prepareStatement(sql);

     stmt.setString(1, txtUsername.getText());
     stmt.setString(2, txtPassword.getText());
     stmt.setString(3, comboBox.getSelectedItem().toString());
     stmt.setInt(4, id);

     stmt.executeUpdate();
    } catch (Exception e2) {
     // TODO: handle exception
    }
    showTable();
   }
  });
  btnUpdate.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\Actions-edit-redo-icon.png"));
  btnUpdate.setBounds(234, 84, 106, 23);
  frame.getContentPane().add(btnUpdate);

  JButton btnDelete = new JButton("Delete");
  btnDelete.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String sql = "DELETE FROM manager.login WHERE id =?";
    int id = Integer.parseInt(txtID.getText());
    try {
     stmt = conn.prepareStatement(sql);
     stmt.setInt(1, id);
     stmt.executeUpdate();

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

   }
  });
  btnDelete.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing003\\icon\\Actions-edit-delete-icon.png"));
  btnDelete.setBounds(234, 111, 106, 23);
  frame.getContentPane().add(btnDelete);

  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setBounds(20, 185, 346, 146);
  frame.getContentPane().add(scrollPane);

  table = new JTable();
  table.addMouseListener(new MouseAdapter() {
   @Override
   public void mouseClicked(MouseEvent arg0) {
    int row = table.getSelectedRow();
    if (row != -1) {
     txtID.setText(table.getValueAt(row, 0).toString());
     txtUsername.setText(table.getValueAt(row, 1).toString());
     txtPassword.setText(table.getValueAt(row, 2).toString());
     comboBox.setSelectedItem(table.getValueAt(row, 3).toString());
    }
   }
  });
  scrollPane.setViewportView(table);
  table.setModel(
    new DefaultTableModel(new Object[][] {}, new String[] { "ID", "Username", "Password", "Roles" }));

  JLabel lblXinChao = new JLabel("Xin chao Admin");
  lblXinChao.setBounds(36, 11, 95, 14);
  frame.getContentPane().add(lblXinChao);

  JLabel lblManagerLogin = new JLabel("Manager Login");
  lblManagerLogin.setFont(new Font("MV Boli", Font.PLAIN, 16));
  lblManagerLogin.setBounds(216, 0, 124, 32);
  frame.getContentPane().add(lblManagerLogin);

  txtSearch = new JTextField();
  txtSearch.setBounds(20, 342, 234, 20);
  frame.getContentPane().add(txtSearch);
  txtSearch.setColumns(10);

  JButton btnSearch = new JButton("Search");
  btnSearch.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String Search = txtSearch.getText().trim();

    Vector cols = new Vector();
    cols.addElement("ID");
    cols.addElement("User");
    cols.addElement("Pass");
    cols.addElement("Roles");

    // Data
    Vector dataSearch = new Vector();
    String sql = "Select * FROM manager.login";
    
    try {
     stmt = conn.prepareStatement(sql);
     rs = stmt.executeQuery();
     while (rs.next()) {
      Vector user = new Vector();
      user.add(rs.getInt("id"));
      user.add(rs.getString("user"));
      user.add(rs.getString("pass"));
      user.add(rs.getString("roles"));

      if (user.toString().toLowerCase().contains(Search)) {
       dataSearch.addElement(user);
      }

     }
    } catch (Exception e2) {
     // TODO: handle exception
    }
    table.setModel(new DefaultTableModel(dataSearch, cols));
   }
  });
  btnSearch.setBounds(277, 342, 89, 23);
  frame.getContentPane().add(btnSearch);

  JButton btnNewButton = new JButton("About");
  btnNewButton
    .setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\Actions-help-about-icon.png"));
  btnNewButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    Note note = new Note();
    note.frame.setVisible(true);
    note.frame.setResizable(false);
   }
  });
  btnNewButton.setBounds(234, 142, 106, 23);
  frame.getContentPane().add(btnNewButton);

 }
}

Note.java  << About create database
package Admin;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class Note {

 public JFrame frame;

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

 /**
  * Create the application.
  */
 public Note() {
  initialize();
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 269, 203);
//  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);
  
  JLabel lblNewLabel = new JLabel("");
  lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Lonely\\workspace\\Swing004\\icon\\2016-04-19_220156.png"));
  lblNewLabel.setBounds(0, 25, 255, 139);
  frame.getContentPane().add(lblNewLabel);
  
  JLabel lblAbout = new JLabel("Database  version 1.0 2016");
  lblAbout.setBounds(49, 11, 178, 14);
  frame.getContentPane().add(lblAbout);
 }
}







Download: https://drive.google.com/open?id=0BzClcKmnT6YbWHI3dnNRV0Zkb0E
Next to Search option comboBox -- search smart 
Next >> http://giai-ma.blogspot.com/2016/04/search-combobox-in-java-gui-swing.html

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang