Database Reference
In-Depth Information
the list by last name of the employee, and we concatenate the last name, a comma, the first name, and
the employee_id in parentheses. We do similar queries in dataInit() to populate the job ID, department
ID, and manager drop-down combo lists. Notice in Listing 12-16 that, before we process the results
through the while(rs.next()) loop, we first remove all existing items in the list and we add a blank string
as the first item. We may call dataInit() repeatedly, after every update, because the lists of values may
have changed as a result of our update. So, we clear the old listings and add new listings during
initialization. We add the blank string as our first item in each list, as the default selection.
Listing 12-16. AddUser Data Initialization Method
private void dataInit() throws Exception {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT last_name || ', ' || first_name || " +
"' (' || employee_id || ')' " +
" FROM hr.v_employees_public ORDER BY last_name");
// This throws event to run existingEmpComboBox_actionPerformed() method
// Calls blankAll()
existingEmpComboBox. removeAllItems() ;
existingEmpComboBox. addItem("") ;
while (rs.next()) {
existingEmpComboBox. addItem(rs.getString(1)) ;
}
if (rs != null)
rs.close();
...
The list in existingEmpComboBox becomes our source for determining which employee the
application user wants to view or edit. When the application user selects an employee, we need to find
the corresponding data in the HR schema, and we want to find that data by employee_id . The plan will be
to pull the employee ID out from between the parentheses of the selected item in existingEmpComboBox ,
so we will create a static utility method named pullIDFromParens() , Listing 12-17, that we will place in a
class named Utility. We will use this in several places in this application, so we want it to be centrally
located and not repeated as a member method of several functional screens.
Listing 12-17. Get Data Index from Data Value
static String pullIDFromParens(String inValue) {
String rtrnValue = "";
try {
int openPlace = inValue.indexOf("(");
int closePlace = inValue.indexOf(")", openPlace);
if (openPlace > -1 && closePlace > -1)
rtrnValue = inValue.substring(openPlace + 1, closePlace);
} catch (Exception x) {
}
return rtrnValue;
}
 
Search WWH ::




Custom Search