2.How to insert data to the database?


Steps:
1.     On EmployeesController.java type the following:
package Controllers;
import Models.*;
import Views.*;
import java.awt.event.*;
public class EmployeesController {
    EmployeesModel model;
    EmployeesView view;
    public EmployeesController(){}
    public EmployeesController(EmployeesModel model,EmployeesView view){
        this.model=model;
        this.view=view;
        view.menu(new btnMenuActionListener());
    }
    class btnMenuActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            String action = e.getActionCommand();
            if(action.equals("signup")){
                view.signup(new btnSignupActionListener());
            }
        }
    }
    class btnSignupActionListener implements ActionListener{
        @Override
         public void actionPerformed(ActionEvent e){
          String action = e.getActionCommand();
          if(action.equals("save")){
                AddEmployee();
            }
         }
    }
    private void AddEmployee(){
        EmployeesModel Employee=view.getEmployee("signup");
        boolean result = model.Save(Employee);
        if(result==true){
            view.SaveMsg();
            view.ResetSignup();
            }
        else{view.ErrMsg();}
   }   
}

2.     On EmployeesView.java type the following:
package Views;
import Models.*;
import java.awt.event.*;
import javax.swing.*;
public class EmployeesView {
    EmployeesModel model;
    MenuView menu;
    SignupView signup;
    public EmployeesView(){}
    public EmployeesView(EmployeesModel model){
    this.model=model;
    }
    public void menu(ActionListener listener){
        menu = new MenuView(listener);
    }
    public void signup(ActionListener listener){
        signup= new SignupView(listener); 
    }
    public EmployeesModel getEmployee(String source){
           return signup.getEmployee();
    }
   public void ResetSignup(){signup.reset();}
   public void SaveMsg(){
       displayMsg("Successfully Saved...");
   }
   public void ErrMsg(){
       displayMsg("Error Encountered...");
   }
   private void displayMsg(String msg){
       JOptionPane.showMessageDialog(null, msg);
   }
   public static void addActionListener(JButton btn, ActionListener listener,String action){
        btn.addActionListener(listener);
        btn.setActionCommand(action);
   }
}

3.     On SignupView.java type the following:
package Views;
import Models.*;
public class SignupView extends javax.swing.JFrame {
    public SignupView(){
        super("Employee Signup");
        initComponents();
    }
    public SignupView(java.awt.event.ActionListener listener) {
        this();
        EmployeesView.addActionListener(btnSave, listener,"save");
        this.setVisible(true);
    }
   
    public void reset(){
        txtName.setText("");
        txtAddress.setText("");
        txtName.requestFocusInWindow();
    }
    public EmployeesModel getEmployee(){
        EmployeesModel Employee=new EmployeesModel();
        Employee.setName(txtName.getText());
        Employee.setAddress(txtAddress.getText());
        return Employee;
    }
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                         
    reset();
}                                        
private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {                                        
    this.dispose();
}

4.     On EmployeesModel.java type the following:
package Models;
import java.sql.*;
public class EmployeesModel extends PersonsModel {
    private int id;
    Connection db;
    public int getId(){return id;}
    public void setId(int id){this.id= id;}
    public EmployeesModel(){}

    public EmployeesModel(Connection db){
    this.db=db;
    }

    public boolean Save(EmployeesModel Employee){
    String sql = "INSERT INTO Employees(name,address)VALUES('"+Employee.getName()+"','"+Employee.getAddress()+"')";
    return MSAccessDB.Insert(sql,db);
    }
}

5.     On PersonsModel.java type the following:
package Models;
public class PersonsModel {
    private String name;
    private String address;
    public void setName(String name){this.name=name;}
    public String getName(){return name;}
    public void setAddress(String address){this.address=address;}
    public String getAddress(){return address;}
    public PersonsModel(){}
}

6.     On MSAccessDB.java type the following:
package Models;
import java.sql.*;
public class MSAccessDB {
    public static Connection Connect(){
        Connection conn=null;
        try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=EmployeesDB.mdb";
        conn=DriverManager.getConnection(database,"","");
        }catch(Exception ex){ex.printStackTrace();}
        return conn;
    }
    public static boolean Insert(String sql,Connection db){
        return Execute(sql,db);
    }
    private static boolean Execute(String sql,Connection db){
        try{
        Statement s = db.createStatement();
        s.execute(sql);
        s.close();
        return true;
        }catch(Exception ex){
                ex.printStackTrace();return false;
        }
    }
}

7.     Put your code to the test, nice jobJ

No comments:

Post a Comment