Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Application;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.BorderLayout;
- import java.awt.Font;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.table.DefaultTableModel;
- public class Work extends JFrame implements ActionListener {
- private JLabel JTitle;
- private JButton JAdd, JUpdate, JDelete, JPremium, JAccept, jDelete2, jPremium,jUpdate;
- private JTextField textId, textFname, textLname, textE_mail, textAddress, textSalary;
- public Work() {
- super("Application");
- // setLocationRelativeTo(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLayout(null);
- this.setExtendedState(JFrame.MAXIMIZED_BOTH);
- setSize(800, 600);
- setVisible(true);
- // Title Label
- JTitle = new JLabel("Business application");
- JTitle.setBounds(600, 20, 200, 20);
- JTitle.setFont(new Font("SansSerif", Font.BOLD, 18));
- add(JTitle);
- // Buttons
- JAdd = new JButton("Add new");
- JAdd.setBounds(900, 100, 100, 20);
- JAdd.addActionListener(this);
- add(JAdd);
- JUpdate = new JButton("Update");
- JUpdate.setBounds(1000, 100, 100, 20);
- JUpdate.addActionListener(this);
- add(JUpdate);
- JDelete = new JButton("Delete");
- JDelete.setBounds(1100, 100, 100, 20);
- JDelete.addActionListener(this);
- add(JDelete);
- JPremium = new JButton("Earnings");
- JPremium.setBounds(1200, 100, 100, 20);
- JPremium.addActionListener(this);
- add(JPremium);
- JAccept = new JButton("Accept");
- JAccept.setBounds(1050, 200, 100, 20);
- JAccept.addActionListener(this);
- JAccept.setVisible(false);
- add(JAccept);
- jDelete2 = new JButton("Delete");
- jDelete2.setBounds(1050, 200, 100, 20);
- jDelete2.addActionListener(this);
- jDelete2.setVisible(false);
- add(jDelete2);
- jPremium = new JButton("Change");
- jPremium.setBounds(1050, 200, 100, 20);
- jPremium.addActionListener(this);
- jPremium.setVisible(false);
- add(jPremium);
- jUpdate = new JButton("Update");
- jUpdate.setBounds(1050, 200, 100, 20);
- jUpdate.addActionListener(this);
- jUpdate.setVisible(false);
- add(jUpdate);
- // textId,, textLname, textE_mail, textAddress, textSalary;
- // textfield
- textFname = new JTextField();
- textFname.setBounds(800, 150, 100, 20);
- add(textFname);
- textFname.setVisible(false);
- textLname = new JTextField();
- textLname.setBounds(900, 150, 100, 20);
- add(textLname);
- textLname.setVisible(false);
- textE_mail = new JTextField();
- textE_mail.setBounds(1000, 150, 100, 20);
- add(textE_mail);
- textE_mail.setVisible(false);
- textAddress = new JTextField();
- textAddress.setBounds(1100, 150, 100, 20);
- add(textAddress);
- textAddress.setVisible(false);
- textSalary = new JTextField();
- textSalary.setBounds(1200, 150, 100, 20);
- add(textSalary);
- textSalary.setVisible(false);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- Object source = e.getSource();
- if (source == JAdd) {
- textFname.setVisible(true);
- textFname.setText("Fname");
- textLname.setVisible(true);
- textLname.setText("Lname");
- textE_mail.setVisible(true);
- textE_mail.setText("e-mail");
- textAddress.setVisible(true);
- textAddress.setText("Address");
- textSalary.setVisible(true);
- textSalary.setText("Salary");
- JAccept.setVisible(true);
- jDelete2.setVisible(false);
- jPremium.setVisible(false);
- jUpdate.setVisible(false);
- }
- if (source == JAccept) {
- AddEmployees();
- validate();
- repaint();
- }
- if(source == JDelete){
- textFname.setVisible(false);
- textLname.setVisible(false);
- textAddress.setVisible(false);
- textSalary.setVisible(false);
- JAccept.setVisible(false);
- jPremium.setVisible(false);
- jUpdate.setVisible(false);
- jDelete2.setVisible(true);
- textE_mail.setVisible(true);
- textE_mail.setText("id");
- }
- if (source == jDelete2) {
- DeleteEmployees();
- validate();
- repaint();
- }
- if(source == JPremium){
- textFname.setVisible(false);
- textLname.setVisible(false);
- textAddress.setVisible(false);
- JAccept.setVisible(false);
- jDelete2.setVisible(false);
- jUpdate.setVisible(false);
- textE_mail.setVisible(true);
- textE_mail.setText("id");
- textSalary.setVisible(true);
- textSalary.setText("Change");
- jPremium.setVisible(true);
- }
- if(source == jPremium){
- PremiumEmployees();
- }
- if(source == JUpdate){
- textFname.setVisible(true);
- textFname.setText("Id");
- textLname.setVisible(true);
- textLname.setText("Fname");
- textE_mail.setVisible(true);
- textE_mail.setText("Lname");
- textAddress.setVisible(true);
- textAddress.setText("e_mail");
- textSalary.setVisible(true);
- textSalary.setText("Address");
- jUpdate.setVisible(true);
- jPremium.setVisible(false);
- JAccept.setVisible(false);
- jDelete2.setVisible(false);
- }
- if(source == jUpdate){
- Update();
- }
- }
- // connection
- static Connection getConnection() {
- Connection con = null;
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return con;
- }
- static ArrayList<Users> getUsers() {
- ArrayList<Users> users = new ArrayList<Users>();
- Connection con = getConnection();
- Statement st;
- ResultSet rs;
- Users u;
- try {
- st = con.createStatement();
- rs = st.executeQuery("Select * from employees");
- while (rs.next()) {
- u = new Users(rs.getInt("id"), rs.getString("fname"), rs.getString("lname"), rs.getString("e_mail"),
- rs.getString("address"), rs.getInt("salary"));
- users.add(u);
- }
- } catch (SQLException ex) {
- Logger.getLogger(Work.class.getName()).log(Level.SEVERE, null, ex);
- }
- return users;
- }
- private void AddEmployees() {
- String pName = textFname.getText();
- String pLname2 = textLname.getText();
- String pE_mail = textE_mail.getText();
- String pAddres = textAddress.getText();
- int pSalary = Integer.parseInt(textSalary.getText());
- try {
- // connection to database
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
- // create a statement
- Statement myStmt = myConn.createStatement();
- // execute sql query
- String sql2 = "insert into employees (fname,lname,e_mail,address,salary) values ('" + pName + "','"
- + pLname2 + "','" + pE_mail + "','" + pAddres + "'," + pSalary + ")";
- myStmt.executeUpdate(sql2);
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- private void DeleteEmployees(){
- int D = Integer.parseInt(textE_mail.getText());
- try {
- // connection to database
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
- // create a statement
- Statement myStmt = myConn.createStatement();
- // execute sql query
- String sql ="DELETE from employees where id="+D;
- myStmt.executeUpdate(sql);
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- private void PremiumEmployees(){
- int id = Integer.parseInt(textE_mail.getText());
- int rise = Integer.parseInt(textSalary.getText());
- try {
- // connection to database
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
- // create a statement
- Statement myStmt = myConn.createStatement();
- // execute sql query
- String sql ="update employees set salary="+rise+" where id ="+id ;
- myStmt.executeUpdate(sql);
- System.out.println("aktualizacja");
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- private void Update(){
- int pId = Integer.parseInt(textFname.getText());
- String pName = textLname.getText();
- String pLname2 = textE_mail.getText();
- String pE_mail = textAddress.getText();
- String pAddress = textSalary.getText();
- try {
- // connection to database
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
- // create a statement
- Statement myStmt = myConn.createStatement();
- // execute sql query
- String sql ="update employees set fname='"+pName+"', lname='"+pLname2+"', e_mail='"+pE_mail+"', address='"+pAddress+"'"+"where id="+pId;
- myStmt.executeUpdate(sql);
- System.out.println("aktualizacja");
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- public static void main(String[] args) {
- JTable table = new JTable();
- DefaultTableModel model = new DefaultTableModel();
- Object[] columnsName = new Object[6];
- columnsName[0] = "Id";
- columnsName[1] = "Fname";
- columnsName[2] = "Lname";
- columnsName[3] = "E_mail";
- columnsName[4] = "Address";
- columnsName[5] = "Salary";
- model.setColumnIdentifiers(columnsName);
- Object[] rowData = new Object[6];
- for (int i = 0; i < getUsers().size(); i++) {
- rowData[0] = getUsers().get(i).getId();
- rowData[1] = getUsers().get(i).getFname();
- rowData[2] = getUsers().get(i).getLname();
- rowData[3] = getUsers().get(i).getE_mail();
- rowData[4] = getUsers().get(i).getAddress();
- rowData[5] = getUsers().get(i).getSalary();
- model.addRow(rowData);
- }
- table.setModel(model);
- System.out.println(getUsers().size());
- JPanel panel = new JPanel();
- // panel.setLayout(new BorderLayout());// responsywne
- JScrollPane pane = new JScrollPane(table);
- // panel.add(pane, BorderLayout.CENTER);
- panel.add(pane);
- panel.setBounds(100, 100, 500, 500);
- // window.setContentPane(panel);
- Work window = new Work();
- window.add(panel);
- window.setVisible(true);
- }
- }
Add Comment
Please, Sign In to add comment