Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
- import java.awt.*;
- import java.sql.*;
- import java.util.*;
- // final project
- class EmpolyeeAll extends JFrame implements ActionListener{
- //driver name and database name
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/emp";
- //database user and pass
- static final String USER = "root";
- static final String PASS = "1234";
- Connection conn = null;
- Statement stmt = null;
- JLabel empTitleLb;
- JLabel idLb, nameLb, salaryLb, postLb,showLb,deleteLb;;
- JTextField idField, nameField, salaryField,deleteField;
- JComboBox postJcb;
- JButton regJb,showJb,deleteJb,updateJb, CencleJb;
- JTable table = new JTable();
- //font
- Font font = new Font("Serif", Font.BOLD, 30);
- Font fontlabel = new Font("Serif", Font.BOLD, 20);
- //frame color
- Color foreColor =new Color(255, 255, 255);
- Color backColor = new Color(120, 20, 102);
- //label color
- Color foreColorlabel =new Color(255, 255, 255);
- Color backColorlabel = new Color(120, 150, 102);
- //field font
- Font fieldFont = new Font("Serif", Font.BOLD, 25);
- Color backColorField = new Color(198, 185, 185);
- Color foreColorField =new Color(0, 0, 0);
- //button
- Color backColorButton = new Color(0, 153, 51);
- EmpolyeeAll(){
- //title
- empTitleLb = new JLabel("Employee Registration Form",JLabel.CENTER);
- empTitleLb.setBounds(170,20,400,40);
- empTitleLb.setFont(font);
- empTitleLb.setOpaque(true);
- empTitleLb.setBackground(backColor);
- empTitleLb.setForeground(foreColor);
- add(empTitleLb);
- //employee id
- idLb = new JLabel("Emp Id : ",JLabel.CENTER);
- idLb.setBounds(150,100,150,30);
- idLb.setFont(fontlabel);
- idLb.setOpaque(true);
- idLb.setBackground(backColorlabel);
- idLb.setForeground(foreColorlabel);
- add(idLb);
- idField = new JTextField();
- idField.setBounds(350,100,200,30);
- idField.setToolTipText("<html><b><font color=gray size=6>"
- + "Enter Emp.ID here" + "</font></b></html>");
- idField.setFont(fieldFont);
- idField.setBackground(backColorField);
- idField.setForeground(foreColorField);
- add(idField);
- //employee name
- nameLb = new JLabel("Name: ",JLabel.CENTER);
- nameLb.setBounds(150,150,150,30);
- nameLb.setFont(fontlabel);
- nameLb.setOpaque(true);
- nameLb.setBackground(backColorlabel);
- nameLb.setForeground(foreColorlabel);
- add(nameLb);
- nameField = new JTextField();
- nameField.setBounds(350,150,200,30);
- nameField.setToolTipText("<html><b><font color=gray size=6>"
- + "Enter Name here" + "</font></b></html>");
- nameField.setFont(fieldFont);
- nameField.setBackground(backColorField);
- nameField.setForeground(foreColorField);
- add(nameField);
- //employee slary
- salaryLb = new JLabel("Salary : ",JLabel.CENTER);
- salaryLb.setBounds(150,200,150,30);
- salaryLb.setFont(fontlabel);
- salaryLb.setOpaque(true);
- salaryLb.setBackground(backColorlabel);
- salaryLb.setForeground(foreColorlabel);
- add(salaryLb);
- salaryField= new JTextField();
- salaryField.setBounds(350,200,200,30);
- salaryField.setToolTipText("<html><b><font color=gray size=6>"
- + "Enter Salary here" + "</font></b></html>");
- salaryField.setFont(fieldFont);
- salaryField.setBackground(backColorField);
- salaryField.setForeground(foreColorField);
- add(salaryField);
- //post combobox
- postLb = new JLabel("Post : ",JLabel.CENTER);
- postLb.setBounds(150,250,150,30);
- postLb.setFont(fontlabel);
- postLb.setOpaque(true);
- postLb.setBackground(backColorlabel);
- postLb.setForeground(foreColorlabel);
- add(postLb);
- String post[] = {"select","Programmer","developer","Designer"};
- postJcb = new JComboBox(post);
- postJcb.setFont(fieldFont);
- postJcb.setBackground(backColorField);
- postJcb.setForeground(foreColorField);
- postJcb.setBounds(350,250,200,30);
- add(postJcb);
- //reg and cencle button
- regJb = new JButton("Registration");
- regJb.setBounds(70,360,160,40);
- regJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
- regJb.setBackground(backColorButton);
- regJb.setForeground(foreColorlabel);
- regJb.addActionListener(this);
- add(regJb);
- //update
- updateJb = new JButton("Update");
- updateJb.setBounds(370,360,120,40);
- updateJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
- updateJb.setBackground(backColorButton);
- updateJb.setForeground(foreColorlabel);
- updateJb.addActionListener(this);
- add(updateJb);
- //delete
- deleteLb = new JLabel("Enter Id ",JLabel.CENTER);
- deleteLb.setBounds(70,420,150,30);
- deleteLb.setFont(fontlabel);
- deleteLb.setOpaque(true);
- deleteLb.setBackground(backColorlabel);
- deleteLb.setForeground(foreColorlabel);
- add(deleteLb);
- deleteField = new JTextField();
- deleteField.setBounds(250,420,70,30);
- deleteField.setToolTipText("<html><b><font color=gray size=6>"
- + "Enter delete ID here" + "</font></b></html>");
- deleteField.setFont(fieldFont);
- deleteField.setBackground(backColorField);
- deleteField.setForeground(foreColorField);
- add(deleteField);
- deleteJb = new JButton("Delete");
- deleteJb.setBounds(350,420,110,30);
- deleteJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
- deleteJb.setBackground(new Color(255, 64, 0));
- deleteJb.setForeground(foreColorlabel);
- deleteJb.addActionListener(this);
- add(deleteJb);
- //show
- showJb= new JButton("Show");
- showJb.setBounds(250,360,100,40);
- showJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
- showJb.setBackground(backColorButton);
- showJb.setForeground(foreColorlabel);
- showJb.addActionListener(this);
- add(showJb);
- //cancle
- CencleJb= new JButton("Cancle");
- CencleJb.setBounds(505,360,120,40);
- CencleJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
- CencleJb.setBackground(Color.RED);
- CencleJb.setForeground(foreColorlabel);
- CencleJb.addActionListener(this);
- add(CencleJb);
- //table panel
- JScrollPane pane = new JScrollPane(table);
- pane.setBounds(650,90,400,400);
- add(pane);
- // show label
- showLb = new JLabel("Show All Employee Info ",JLabel.CENTER);
- showLb.setBounds(650,30,400,40);
- showLb.setFont(fontlabel);
- showLb.setOpaque(true);
- showLb.setBackground(new Color(102, 51, 0));
- showLb.setForeground(Color.GREEN);
- add(showLb);
- //frame
- setTitle("DEMO Project : Employee ");
- setSize(1100,600);
- getContentPane().setBackground(new Color(255, 165, 0));
- setLocationRelativeTo(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(null);
- setVisible(true);
- }
- // ************************action performed ****************************
- public void actionPerformed(ActionEvent ae){
- int id = 0,salary=0;
- String name="",post="";
- if(ae.getSource()==regJb){
- //geting value from text field
- if(postJcb.getSelectedItem().equals("select")|| idField.getText().isEmpty() || nameField.getText().isEmpty()||salaryField.getText().isEmpty() ){
- JOptionPane.showMessageDialog(null,"Please check all field");
- }else if(salaryField.getText().equals("0")){
- JOptionPane.showMessageDialog(null," please check salary ");
- }
- else{
- //idField, nameField, salaryField;
- try{
- id =Integer.parseInt(idField.getText());
- name = nameField.getText();
- salary = Integer.parseInt(salaryField.getText());
- post = (String) postJcb.getSelectedItem();
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null," data submit error "+e+"");
- }
- //database handle here
- try{
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- stmt = conn.createStatement();
- //check salary and id 0;
- if(0 >=id || salary <=0){
- JOptionPane.showMessageDialog(null," please check id and Salary Field ");
- //
- }else{
- //String sql = "select * from empinfo where id = '"+id+"'";
- String sql = "insert into empinfo values('"+id+"','"+name+"','"+salary+"','"+post+"')";
- //ResultSet rs = stmt.executeQuery(sql);
- stmt.executeUpdate(sql);
- JOptionPane.showMessageDialog(null," Successfully data submit");
- }
- }catch(Exception e){
- JOptionPane.showMessageDialog(null," Duplicate Employee ID "+id+"\nPlease Enter Unique ID ");
- System.out.println(e);
- }
- if (conn != null||stmt != null ) {
- try {
- conn.close();
- stmt.close();
- } catch (SQLException e) {
- }
- }
- }
- }
- //Show button
- if(ae.getSource()==showJb){
- //String show = "SELECT * FROM empinfo";
- try{
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM empinfo");
- Object []columns = {"Emp Id","Name ","Salary "," Post"};
- DefaultTableModel model = new DefaultTableModel();
- model.setColumnIdentifiers(columns);
- table.setModel(model);
- table.setRowHeight(30);
- table.setBackground(new Color(204, 204, 204));
- table.setForeground(new Color(0, 0, 0));
- Object[] row = new Object[4];
- while(rs.next()){
- row[0] = rs.getInt("id");
- row[1] = rs.getString("name");
- row[2] = rs.getInt("salary");
- row[3] = rs.getString("post");
- //showLb.setText(Integer.toString(rs.getInt("id")));
- model.addRow(row);
- }
- }catch(Exception e){
- System.out.println("show error"+e);
- }
- }
- //delete button
- if(ae.getSource()==deleteJb){
- try{
- stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM empinfo");
- int dele = Integer.parseInt(deleteField.getText());
- while(rs.next()){
- //checking id
- if(dele==rs.getInt("id")){
- String del = "DELETE FROM empinfo WHERE id = '"+dele+"'";
- stmt.executeUpdate(del);
- JOptionPane.showMessageDialog(null," Deleted Successfully");
- }
- }
- JOptionPane.showMessageDialog(null," Plese Enter Correct ID ");
- // deleteField.setText("delete");
- }catch(Exception ee){
- JOptionPane.showMessageDialog(null," Please Enter Integer Number:\n"+ee);
- }
- }
- //update button
- if(ae.getSource()==updateJb){
- JOptionPane.showMessageDialog(null," Update button will work soon :( ");
- }
- //cancle button
- if(ae.getSource()==CencleJb){
- //idField, nameField, salaryField;
- idField.setText(null);
- nameField.setText(null);
- salaryField.setText(null);
- deleteField.setText(null);
- postJcb.setSelectedItem("select");
- }
- }
- public static void main(String argd[]){
- EmpolyeeAll em = new EmpolyeeAll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement