Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

populate data

Posted on 2011-02-15
10
Medium Priority
?
287 Views
Last Modified: 2012-05-11
I want to present data  in my jsp page as follows...


project1
-subproject1
-subproject2

project2
-subproject1
-subproject2
-subproject3





My SQL query from database is returning rows like this..

project1,subproject1
project1,subproject2
project2,subproject1
project2,subproject2
project2,subproject3

Question:  Which way I need to populate the data so that I can display data in jsp as show above ?
0
Comment
Question by:cofactor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 500 total points
ID: 34895678
you need to use a hashmap, with the key being the project id (project1, project2 etc), and the value being the list of subprojects.

Map<String, List<String>>();

you can then iterate the map (along with list inside it) in JSP to show this data.
0
 

Author Comment

by:cofactor
ID: 34895798
>>>with the key being the project id (project1, project2 etc), and the value being the list of >>subprojects.

How do  you  get  the key and the  corresponding list of subprojects from the below resultset ?


My SQL query from database is returning rows like this..

project1,subproject1
project1,subproject2
project2,subproject1
project2,subproject2
project2,subproject3
0
 

Expert Comment

by:kabanzafer
ID: 34895849
I don't know the structure of your code but you can also use an object oriented approach in your db classes. For example you may have a class named Project encapsulating SubProject classes extending the super class as a list. Such as the code below. Then you may iterate over a list of Project classes in your jsp. There are many ways but you need to choose the one that fits your code structure most. Hope this helps.
   
public class Project[
  String name;
  List<Project> subClasses;
  ...
}

public class SubProject extends Project{
   ....
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:kabanzafer
ID: 34895875
While iterating in the resultset you have to check the list you are holding for the projects. If you have that project you have to insert into the subproject list of that project; if you don't, you have to create a project with a list containing the current subproject  
0
 
LVL 4

Expert Comment

by:msk_apk
ID: 34897386
normally its not advisable to write java code in jsp. if ur allowed to do so, then following logic can be used. this is just a pseudo code only, suitable methods can be added I believe.

<%
String currentProject = null;

for(eachRow in Rows)
{
        String tempProject = row.getElement(0);
        String  subproject = row.getElement(1);
        if(tempProject.equals(currentProject))
        { %>
       <%= subproject %>
         
       <% } else { currentProject = tempProject; %>
       <%= tempProject %>
       <% = subproject %>
   
<%} %>

if this has to be used in multiple places this can be added as Bean and <jsp:useBean> tag can be used to avoid java code.
0
 

Author Comment

by:cofactor
ID: 34899046
@kabanzafer
your code is erroneous . you have List<Project> subClasses;  inside a Project class  ... this is wrong.  A Project has a list of subprojects.

@msk_apk
I dont like your code .Also, I can not afford your jsp code.  This is pretty inconvenient . I wish to populate the data in VO classes and then iterate the VO  .

 
0
 

Expert Comment

by:kabanzafer
ID: 34900346
I assume subprojects extends projects so List<Projects> is not wrong there. That's called polymorphism in OO programming.
0
 
LVL 17

Expert Comment

by:gops1
ID: 34913753
You can also think about getting only the project, instead of getting the project and sub project together. Once you have the main project on page, can use Ajax approach to get the sub project. That also improves the performance.
0
 

Author Comment

by:cofactor
ID: 34938292
>>>Once you have the main project on page, can use Ajax approach to get the sub project.

In that case there will be several  Ajax calls ...is not it ?  

As my MySQL query is returning project and sub project together , cant we just put them into VO class and display in the jsp.

 I'm stuck with the data population into VO so that for a given project ,I get the subprojects .
0
 

Author Closing Comment

by:cofactor
ID: 35727709
Excellent
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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

722 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