Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

java data structure

I have a issue with data structure. Please find the file attached . I have explained in detail. This involves JDBC and JSP.
display.doc
Avatar of for_yan
for_yan
Flag of United States of America image


There are two ways to do it - one is to modify SQL query, so
that it conactenates responsibilityes for the same users - that requires a little bit of
work with query but probably will not need any changes in JSP.

Another variant is to conactente these responsibilties in Java code
generating JSP. Which variant do you prefer?

This link gives some methods how to modfy query:
http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string

What type of database do you have - is it Oracle or MySql, or something else?
If you want to do it on JSP side, perhpas you can post your jsp code

This is one more example how to do it using Oracle query;
these queries do not look very simple, but they work
http://stackoverflow.com/questions/468990/how-can-i-combine-multiple-rows-into-a-comma-delimited-list-in-oracle
Avatar of cofactor
cofactor

ASKER

>>>If you want to do it on JSP side, perhpas you can post your jsp code

jsp is simple.
Made a VO list and iterator to  display in table. I have posted the java code in the attachment . Please check that.   I prefer java side solution.


Try this query - if you have Oracle 9 it will work for you
I called table tstconcat - just change table name and it will work:
select userid, max(username) username,
substr(replace(replace
(xmlagg(xmlelement("x",responsibility) order by responsibility),'</x>'),'<x>',','),2) responsibility, max(email) email
from tstconcat
group by userid;

Open in new window

I didn't see any code other than retrieval from the database in the doc file

If you replace the query by the one abocve this same JDBC code should get you exactly
what you want
Avatar of CEHJ
For a Java-only solution, give your VO a List of responsibilities and use a Map:
Map<String, UserVO> vos = new HashMap<String, UserVo>();
	while (rs.next()) {
	    String userId = rs.getString("userid");
	    String username = rs.getString("username");
	    String responsibility = rs.getString("responsibility");
	    String email = rs.getString("email");

	    UserVO uvo = vos.get(userId);
	    if (uvo == null) {
		uvo = new UserVO();
		uvo.setUserId(userId);
		uvo.setUsername(username);
		uvo.setEmail(email);
		uvo.getResponsibilties().add(responsibility);
	    }
	    else {
		uvo.getResponsibilties().add(responsibility);
	    }
	}

..............

    class UserVo {
	private List<String> responsibilities = new ArrayList<String>();
        ...........
    }

Open in new window

Sorry - don't forget to call put
uvo.setUserId(userId);
vos.put(userId, uvo);

Open in new window


This is the full code to do it in Java, but with query
change it is much easier

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

public class UserResponsibilities {
    
    public UserResponsibilities(){
             try{
        ResultSet rs = null;

                 HashMap h = new HashMap();    
                 HashMap n = new HashMap();
                 HashMap e = new HashMap();
                 ArrayList userids = new ArrayList();
while (rs.next()) {
String userId = rs.getString("userid");
String username = rs.getString("username");
String responsibility = rs.getString("responsibility");
String email = rs.getString("email");
    n.put(userId, username);
    e.put(userId, email);
    if(!userids.contains(userId))  userids.add(userId);
    
    if(h.get(userId) != null){
        ArrayList al = (ArrayList)h.get(userId);
        al.add(responsibility);
         h.put(userId, al);
    } else
    {
        ArrayList al = new ArrayList();
          al.add(responsibility);
         h.put(userId, al);
    }
         
//populate  in a VO and make a list of VO's and  display in JSP
}
                 
           
                 for(int jj=0; jj<userids.size(); jj++){
                     String userid = (String) userids.get(jj);
                     String username = (String) n.get(userid);
                     String email = (String)e.get(userid);
                     
                     ArrayList al = (ArrayList) h.get(userid);
                     String resp = "";
                     for(int j=0; j<al.size(); j++){
                         
                         resp += (String)al.get(j) + ",";
                         
                         
                     }
               resp = resp.substring(0,resp.size()-1);
                     
                     System.out.println(userid + " " + username + " " + resp + " " + email );
                     
                     
                 }
                 
    
             }catch(Exception ex){
                 
             }
    }
    

    
}

Open in new window

>>>Sorry - don't forget to call put
where you plan to call  put ?

Could you please post the complete code once again ?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent.
Excellent
:)