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
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:
EmployeeDetails
class to EmployeeDetail
(singular). That way, collections of EmployeeDetail
objects can use names like employeeDetails
(plural). getEmployeesByName
since you don't guarantee there's only one employee returned.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.