Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

populate data

Posted on 2011-02-15
10
Medium Priority
?
289 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
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
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!

 

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

Technology Partners: 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

971 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