Solved

populate data

Posted on 2011-02-15
10
282 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
Glassfish admin console not working 1 36
Eclipse Java import and method not resolved 4 52
collection output issue 9 39
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 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