I have some values return from database as a result set like following
**resource_name menu_name menu_group_name**
DepartmentAction Department Admin Operation
PositionAction Position Admin Operation
FoodHabitAction FoodHabits Admin Operation
ReligiousAction Religious Admin Operation
NationalitiesAction Nationlities Admin Operation
I would like to group resource_name and menu_mane based on menu_group_name
some thing like the following
if the menu_group_name is same than group all the corresponding
resource_name and menu_mane against to menu_group_name .
UPDATE :
public Map<String,List> getMenuForLoggedinRole(int roleid){
Map<String,List> menuListMap = new LinkedHashMap<String,List>();
List<MenuListViewModel> menuNamesList = new ArrayList<MenuListViewModel>();
MenuListViewModel menuViewModel;
Connection connection = getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
if (connection != null) {
try {
ps = connection.prepareStatement(" select ar.resource_name,ar.menu_name,mg.menu_group_name "
+ " from m_application_resources as ar,"
+ " m_menu_groups as mg,m_access_matrix as amatrix "
+ " where ar.resourceid = amatrix.resourceid and amatrix.roleid=?");
ps.setInt(1, roleid);
rs = ps.executeQuery();
if(rs.next()) {
String menu_group_name = rs.getString("menu_group_name");
String resource_name = rs.getString("resource_name");
String menu_name = rs.getString("menu_name");
if(menuListMap.containsKey(menu_group_name)){
menuNamesList =(List) menuListMap.get(menu_group_name);
menuViewModel = new MenuListViewModel();
menuViewModel.setResource_name(resource_name);
menuViewModel.setMenu_name(menu_name);
menuNamesList.add(menuViewModel);
menuListMap.put(menu_group_name, menuNamesList);
}else{
menuViewModel = new MenuListViewModel();
menuViewModel.setResource_name(resource_name);
menuViewModel.setMenu_name(menu_name);
menuNamesList.add(menuViewModel);
menuListMap.put(menu_group_name, menuNamesList);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}finally {
try {
closeConnection(connection, rs, ps);
} catch (Exception ex) {
ex.printStackTrace();
//use logger here
}
}
}
printMap(menuListMap);
return menuListMap;
}
when i printed and saw only one value is getting printed others are not.
Please help me how to do this.
i know that it is very simple needs your help.
Regards
create some class,CustomClass, with members
resource_nameandmenu_namenow create a
map<String, List<CustomClass>>iterate over resultset and keep adding data to map
menu_group_namewill be your key and customclass you have created will be value, while putting data in map, check if that key exists, if yes retrive list and add new customclass to it else create a new list with your customclass and add put key value pair