• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

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
0
cofactor
Asked:
cofactor
  • 7
  • 4
  • 4
1 Solution
 
for_yanCommented:

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?
0
 
for_yanCommented:

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?
0
 
for_yanCommented:
If you want to do it on JSP side, perhpas you can post your jsp code
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
for_yanCommented:

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
0
 
cofactorAuthor Commented:
>>>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.
0
 
for_yanCommented:


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

0
 
for_yanCommented:
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
0
 
CEHJCommented:
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

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

Open in new window

0
 
for_yanCommented:

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

0
 
cofactorAuthor Commented:
>>>Sorry - don't forget to call put
where you plan to call  put ?

Could you please post the complete code once again ?
0
 
CEHJCommented:
Sure
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();
vos.put(userId, uvo); //<< put here
		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

0
 
cofactorAuthor Commented:
Excellent.
0
 
cofactorAuthor Commented:
Excellent
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now