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("empList")){
view.empList(new btnEmpListActionListener());
view.Employees();
}
}
}
class btnEmpListActionListener
implements ActionListener{
public void
actionPerformed(ActionEvent e){
view.closeEmpList();
}
}
}
2.
On
EmployeesView.java type the
following:
package Views;
import Models.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class
EmployeesView {
EmployeesModel
model;
MenuView menu;
EmpListView
empList;
public EmployeesView(){}
public EmployeesView(EmployeesModel model){
this.model=model;
}
public void
menu(ActionListener listener){
menu = new MenuView(listener);
}
public void
empList(ActionListener listener){
empList= new EmpListView(listener);
}
public void
Employees(){
ArrayList<EmployeesModel> Employees = model.GetEmployees();
empList.display(Employees);
}
public void
closeEmpList(){
empList.dispose();
}
public static void addActionListener(JButton btn, ActionListener
listener,String action){
btn.addActionListener(listener);
btn.setActionCommand(action);
}
}
3.
On
EmpListView.java type the following:
package Views;
import Models.*;
public class
EmpListView extends javax.swing.JFrame {
public
EmpListView() {
super("Employees");
initComponents();
}
public EmpListView(java.awt.event.ActionListener
listener){
this();
EmployeesView.addActionListener(btnClose, listener,"close");
this.setVisible(true);
}
public void
display(java.util.ArrayList<EmployeesModel> Employees){
javax.swing.table.DefaultTableModel tblModel = new javax.swing.table.DefaultTableModel();
tblEmployees.setModel(tblModel);
tblModel.setColumnIdentifiers(new
String[] {"ID","Name", "Address"});
for(EmployeesModel employee:Employees){
tblModel.addRow(new String[]{""+employee.getId(),employee.getName(),employee.getAddress()});
}
}
}
4.
On
EmployeesModel.java type the
following:
package Models;
import java.sql.*;
import java.util.*;
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 ArrayList<EmployeesModel>
GetEmployees(){
String sql
= "SELECT * FROM Employees";
ResultSet
rs= MSAccessDB.getResults(sql,db);
ArrayList<EmployeesModel> Employees
= new ArrayList<EmployeesModel>();
try{
while(rs!=null
&& rs.next()){
Employees.add(Employee(rs));
}
}catch(Exception ex){ex.printStackTrace();}
return Employees;
}
private EmployeesModel
Employee(ResultSet rs){
EmployeesModel Employee=new
EmployeesModel();
try{
Employee.setId(Integer.parseInt(rs.getString("id")));
Employee.setName(rs.getString("name"));
Employee.setAddress(rs.getString("address"));
}catch(Exception ex){ex.printStackTrace();}
return Employee;
}
}
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
ResultSet getResults(String sql,Connection db){
return Query(sql,db);
}
private static
ResultSet Query(String sql,Connection db){
ResultSet rs=null;
try{
Statement s
= db.createStatement();
s.execute(sql);
rs =
s.getResultSet();
}catch(Exception ex){ex.printStackTrace();}
return rs;
}
}
7.
Put
your code to the test, nice jobJ
No comments:
Post a Comment