?
Solved

java data structure

Posted on 2011-04-20
15
Medium Priority
?
435 Views
Last Modified: 2012-05-11
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
Comment
Question by:cofactor
  • 7
  • 4
  • 4
15 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35431001

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
 
LVL 47

Expert Comment

by:for_yan
ID: 35431016

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
 
LVL 47

Expert Comment

by:for_yan
ID: 35431025
If you want to do it on JSP side, perhpas you can post your jsp code
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 47

Expert Comment

by:for_yan
ID: 35431036

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
 

Author Comment

by:cofactor
ID: 35431313
>>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 35431330


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
 
LVL 47

Expert Comment

by:for_yan
ID: 35431342
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35431346
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35431353
Sorry - don't forget to call put
uvo.setUserId(userId);
vos.put(userId, uvo);

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35431456

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
 

Author Comment

by:cofactor
ID: 35431518
>>>Sorry - don't forget to call put
where you plan to call  put ?

Could you please post the complete code once again ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 35431538
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
 

Author Comment

by:cofactor
ID: 35431765
Excellent.
0
 

Author Closing Comment

by:cofactor
ID: 35431768
Excellent
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35431891
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question