0

I am querying a database to return a list of values based on an input string; and these list of values are displayed in a searchbox for the user. The issue is, my returned lists are being displayed as objects instead of a list of names. Attached are snippets of code to illustrate my point

my retrieve method

public List<EmployeeDetails> getEmployeeByName(String employeeName) {

    List<EmployeeDetails> list=new ArrayList<EmployeeDetails>();
    Connection c=null;
    String sql=("SELECT * FROM  employee_table WHERE UPPER(employeeName) LIKE ? ORDER BY employeeName");

    try{
        c = ConnectionHelper.getConnection();
        PreparedStatement ps=c.prepareStatement(sql);
        ps.setString(1, "%" + employeeName.toUpperCase() + "%");
        ResultSet rs=ps.executeQuery();
        while(rs.next()){


            list.add(new EmployeeDetails  (rs.getInt("employeeid"),
                    rs.getString("employeeName"),
                    rs.getString("employeeAddress"),
                    rs.getString("employeeAge"), 
                    rs.getString("nationality"), 
                    rs.getString("salaryRate")));



        }



    }catch (SQLException e) {
        e.printStackTrace();
        try {
            throw new Exception(e);
        } catch (Exception e1) {

            e1.printStackTrace();
        }
    } finally {
        ConnectionHelper.close(c);
    }

    return **list**;




}

i intend the searchbox be populated with only the names of the returned objects, not the raw encapsulated object. How do i go about this? Any help/pointers would be highly appreciated

Kermit_ice_tea
  • 508
  • 2
  • 8
  • 16

1 Answers1

0

The result of this method must be getting used somewhere. Wherever that is, you need to do something like this:

class EmployeeDAO
{
    // Your method doing data access
    public List<EmployeeDetail> getEmployeeByName(String employeeName) {
        ...
        return list;
    }
}

class EmployeeController
{
    public void searchByName( String employeeName )
    {
        List<EmployeeDetail> employeeDetails = employeeDao.getEmployeeByName( employeeName );

        Map<String, EmployeeDetail> searchResults = new HashMap<String, EmployeeDetail>();
        // Use employeeNames in the search box
        for( EmployeeDetail employeeDetail : employeeDetails )
        {
            searchResults.put( employeeDetail.getEmployeeName(), employeeDetail );
        }

        // I don't know how to return objects for Flex/BlazeDS, so this is more Spring MVC style
        view.put( "searchResults", searchResults );
    }   
}

Two other suggestions:

  1. Rename EmployeeDetails class to EmployeeDetail (singular). That way, collections of EmployeeDetail objects can use names like employeeDetails (plural).
  2. Rename your method to getEmployeesByName since you don't guarantee there's only one employee returned.
Steven Hood
  • 678
  • 5
  • 18
  • Many thanks. The above concept does output the names, but discards its other attributes. The idea being names are only supposed to be displayed, but is still a wrapped object, so that any further action can be carried out on the object in the front-end – Kermit_ice_tea Apr 08 '13 at 16:33
  • Right, I cut the example way down because big code snippets get unwieldly. You would get the full list back to the front-end, and when you build the list just for the search box, you process it as I suggested. Other places in the frontend could continue to iterate the `List` as needed. – Steven Hood Apr 09 '13 at 00:02
  • **chuckles** @steve...you hit it right on the head. What you elaborated is exactly what i need; and that is why i was here in the first place. If i knew how to get it done, we wont be having this conversation. Sooooooo..(code )pointers please – Kermit_ice_tea Apr 10 '13 at 02:41
  • Can you add something from your front-end code so I can see how to fit in what I'm talking about in a way that makes sense to you? Java code and JSP/template code would be helpful for me when editing my answer. – Steven Hood Apr 10 '13 at 03:35
  • thing is, my front-end is designed with flex-blazeds... The flex front-end is configured to bind the returned object to output boxes...The search box is supposed to display the list of items returned and once an item is clicked, it fills the texboxes. I feel all processing should be done in the back-end. I fiddled with the idea of using a hash-map to link the names and object as a key-value pair. – Kermit_ice_tea Apr 10 '13 at 03:46
  • I'm not at all familiar with Flex and BlazeDS, so I can't be too much help there. It sounds like some variation of `HashMap` is reasonable. I've edited my answer to incorporate a little bit of that. Typically, I would expect the full population after selecting a specific employee to be a second request to the web server, perhaps via AJAX, but I don't fully know your requirements. – Steven Hood Apr 10 '13 at 03:57