Solved

populate data

Posted on 2011-02-15
10
281 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factorial example 4 39
how to install java on RHEL image on EC2 4 26
maven module vs maven project 3 21
Which non-HTML GUI front end to use with Java? 3 21
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

786 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