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("update")){
view.update(new btnEditDeleteActionListener(),"Update");
}
}
}
class btnEditDeleteActionListener
implements ActionListener{
@Override
public void
actionPerformed(ActionEvent e){
String action
= e.getActionCommand();
if(action.equals("search")){
int id = view.getEmpID();
SearchEmployee(id);
}
else if(action.equals("Update")){
EmployeesModel Employee=view.getEmployee("editDelete");
Update(Employee);
}
}
}
private void
SearchEmployee(int id){
if(model.isFound(id)){
view.displayEmployee(model.getEmployee(id));
}else{
view.NotFoundMsg();
}
}
private void
Update(EmployeesModel Employee){
boolean result
= model.update(Employee);
if(result==true){
view.UpdateMsg();
view.ResetEmpEditDelete();
}
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;
EmpEditDeleteView
empEditDelete;
public EmployeesView(){}
public EmployeesView(EmployeesModel model){
this.model=model;
}
public void
menu(ActionListener listener){
menu = new MenuView(listener);
}
public void
update(ActionListener listener, String btnText){
empEditDelete
= new
EmpEditDeleteView(listener,btnText);
}
public EmployeesModel getEmployee(String source){
return empEditDelete.getEmployee();
}
public int
getEmpID(){
return empEditDelete.getEmpID();
}
public void
displayEmployee(EmployeesModel Employee){
empEditDelete.display(Employee);
}
public void
ResetEmpEditDelete(){empEditDelete.reset();}
public void ErrMsg(){
displayMsg("Error Encountered...");
}
public void
NotFoundMsg(){
displayMsg("Not Found...");
}
public void
UpdateMsg(){
displayMsg("Successfully Updated...");
}
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
EmpEditDeleteView.java type the
following:
package Views;
import Models.*;
public class
EmpEditDeleteView extends javax.swing.JFrame
{
public
EmpEditDeleteView() {
super("Employee Edit/Delete");
initComponents();
}
public EmpEditDeleteView(java.awt.event.ActionListener listener,String btnText){
this();
btnAction.setText(btnText);
EmployeesView.addActionListener(btnAction,
listener,btnAction.getText());
EmployeesView.addActionListener(btnSearch, listener,"search");
this.setVisible(true);
}
public void
setBtnText(String text){
btnAction.setText(text);
}
public void reset(){
txtID.setText("");
txtName.setText("");
txtAddress.setText("");
txtID.requestFocusInWindow();
}
public int
getEmpID(){
return Integer.parseInt(txtID.getText());
}
public EmployeesModel getEmployee(){
EmployeesModel
Employee=new EmployeesModel();
Employee.setId(Integer.parseInt(txtID.getText()));
Employee.setName(txtName.getText());
Employee.setAddress(txtAddress.getText());
return Employee;
}
public void
display(EmployeesModel Employee){
txtName.setText(Employee.getName());
txtAddress.setText(Employee.getAddress());
}
}
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 EmployeesModel
getEmployee(int id){
ResultSet rs
=Search(id);
EmployeesModel emp=null;
try{
while(rs!=null
&& rs.next()){emp=Employee(rs);}
}catch(Exception ex){ex.printStackTrace();}
return emp;
}
public ResultSet Search(int
id){
String sql="SELECT
* FROM Employees WHERE id="+id;
ResultSet rs
= MSAccessDB.getResults(sql,db);
return rs;
}
public boolean update(EmployeesModel
Employee){
String sql
= "UPDATE Employees SET name='"+Employee.getName()+"',"+
" address='"+Employee.getAddress()+"'"+
" WHERE id="+Employee.getId();
return MSAccessDB.Update(sql,db);
}
public boolean isFound(int
id){
ResultSet rs
=Search(id);
boolean
found=false;
try{
while(rs.next()){found=true;}
}catch(Exception ex){ex.printStackTrace();}
return found;
}
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);
}
public static boolean
Update(String sql,Connection db){
return Execute(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;
}
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
Sir asa na gkan ang btnAction sa EmpEditDeleteView?
ReplyDelete