Solved

populate data

Posted on 2011-02-15
10
279 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:
gurvinder372 earned 125 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now