Java Reference
In-Depth Information
private String createValuesClause() {
String values = "VALUES ('" +
empNum + "', '" +
empName + "', '" +
empStreet + "', '" +
empCity + "', '" +
empState + "', '" +
empZip + "', " +
payRate + ", " +
exemptions + ") ";
return values;
}
private String createSetClause() {
String set = "SET " +
"empNum = '" + empNum + "', " +
"empName = '" + empName + "', " +
"street = '" + empStreet + "', " +
"city = '" + empCity + "', " +
"state = '" + empState + "', " +
"zip = '" + empZip + "', " +
"payRate = " + payRate + ", " +
"exempts = " + exemptions + " ";
return set;
}
private String createWhereClause() {
String where = "WHERE empNum = '" + empNum+ "' ";
return where;
}
Notice that only three of the new methods are public. These public methods are the interface that clients of
the Employee class use to perform the insert, update, and delete functions. Data encapsulation dictates that the
encapsulating class should provide public CRUD (Create, Read, Update, and Delete) functions for other classes to
access the data. The getEmpInfo method provides the read function, and the constructors provide the create function.
We just added the doInsert, doUpdate, and doDelete methods to provide the UD in CRUD.
In actuality, the three public classes simply build the SQL statement string and then pass that string to the private
methods doSelect(String) or doUpdate(String) for execution. Looking even closer, you will see that we have split the
creation of the set , values , and where clauses into separate methods. Because updates, deletes, inserts, and selects
may use the same clauses, we created separate methods so we only define the clauses once. Notice that the createXxxx
methods do not use the public getters and setters to access the property values, they access the private variables
directly. Because the methods are private, there is no danger in modifying the private variables directly. We will now
enable the Insert and Update functions in EnterEmpInfo.
10.
In EnterEmpInfo's setEmployeeProperties method add the following to the beginning of
the method
emp.setEmpNum(empNumTF.getText());
11.
In EnterEmpInfo's actionPerformed method, change:
if (actionBtn == e.getSource()) {
EmployeeFrame ef = new EmployeeFrame(emp);
 
Search WWH ::




Custom Search