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

x
?
Solved

populate records into VO

Posted on 2011-02-24
17
Medium Priority
?
309 Views
Last Modified: 2012-05-11
SQL query returns records as ..

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


I wish to populate this data into my VO class.

My VO class looks like this ..

class Project
{
private String name
// getter
//setter

List Subproject  // this is subproject
// getter
//setter
}

Whats the best way to populate this VO ?


Here is where I'm stuck


                 while (rs.next()) {
            
                  project = rs.getString(1);
                  subproject = rs.getString(2);
                       
                    // How do I populate the VO now ?
                    // Whats the best way to populate the VO ?

                  }
0
Comment
Question by:cofactor
  • 8
  • 8
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34970504
Keep a record of the current value of the first column:
String colOneCurrent = "";

while(rs.next()) {
    String colOne = rs.getString(1);
    String colTwo = rs.getString(2);
    if (!colOne.equals(colOneCurrent)) {
        // Make new VO
        colOneCurrent = colOne;// Set new
    }
    else {
        // Same VO - add colTwo to list
    }
}

Open in new window

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34970509
Try this:

Map<String, Project> map = new HashMap<String, Project>();

while (rs.next()) {
           
   project = rs.getString(1);
   subproject = rs.getString(2);

   Project p = map.get(project);
   if (p == null) {
      p = new Project();
      p.setName(project);
      map.put(project, p);
   }              
   p.getSubprojects().add(subproject);        
}
 
List<Project> projects = new ArrayList<Project>(map.values());
0
 

Author Comment

by:cofactor
ID: 34971952
>>   p.getSubprojects().add(subproject);  

I think, this will  throw NullPointerException.   Where do you initialize the List of Subproject   ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:ioanton
ID: 34972031
In the Project class:

List subprojects = new ArrayList();
0
 

Author Comment

by:cofactor
ID: 34976241
>>In the Project class:

No. I can not do there. I'm re-using the class. This is been used elsewhere. I have to do that in my logic.Where is the appropriate place ?
0
 

Author Comment

by:cofactor
ID: 34977081
CEHJ,

Could you please modify your code . I'm having difficulty populating the Subprojects in the else part.

something is missing in your code.


At the end  I would like to get List<Project> projects as similar to ioanton .  Could  you please modify your code so that it looks a better picture now.  I'm not sure how you are populating Project and Subproject List and so I'm in confusion.

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34977625
Well, here is a workaround:

Map<String, Project> map = new HashMap<String, Project>();

while (rs.next()) {
           
   project = rs.getString(1);
   subproject = rs.getString(2);

   Project p = map.get(project);
   if (p == null) {
      p = new Project();
      p.setName(project);
      map.put(project, p);
   }              

   List subprojects = p.getSubprojects();

   if(subprojects == null) {
      subprojects == new ArrayList();
      p.setSubprojects(subprojects);
   }

   subprojects.add(subproject);
}
 
List<Project> projects = new ArrayList<Project>(map.values());
0
 

Author Comment

by:cofactor
ID: 34979372
That looks brilliant.

I have a  snippet of mockup  HTML  table  [code attached]

I wish to populate this mockup with data from my  Project VO class. Where do I put the loop so that it could generate the display properly.

In jsp , I get  List Projects = session.getAttribute("Projects")  // Projects is the list of Project VO's

I'm stuck with the looping .


..............
<tr>
                                                            <td rowspan="3" valign="middle" class="table_base_bg">Project1</td>
                                                            <td valign="middle" class="table_base_bg">1. Subproject1</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">2. Subproject2</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">3. Subproject3</td>
                                                          </tr>



<tr>
                                                            <td rowspan="3" valign="middle" class="table_base_bg">Project2</td>
                                                            <td valign="middle" class="table_base_bg">1. Subproject1</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">2. Subproject2</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">3. Subproject3</td>
                                                          </tr>
................

Open in new window

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34979685
Are you using JSTL or just scriptlets?
0
 

Author Comment

by:cofactor
ID: 34985918
>>Are you using JSTL or just scriptlets?

Neither. I'll be using Struts 2 tag    i.e   <s:iterator>  tag.

If you are aware of Struts 2 tags , then its excellent. But , if you  dont know Struts 2 tags  ,  could you just show me using scriptlets ?  I'm stuck with the looping . see the <tr>,<td>'s carefully . Where do you put the loop ? this is the tricky part.


0
 
LVL 9

Expert Comment

by:ioanton
ID: 34986273
Here is a JSTL approach, whose output is the same as your html:

<c:forEach var="project" items=${projects}>
      <tr>
            <td valign="middle" class="table_base_bg">${project.name}</td>
            <td>
                  <table width="100%">
                        <c:forEach var="subproject" items="${project.subprojects}" varStatus="index">
                              <tr>
                                    <td>${index + 1}. ${subproject}</td>
                              </tr>
                        </c:forEach>                        
                  </table>
            </td>
      </tr>
</c:forEach>
0
 

Author Comment

by:cofactor
ID: 34987013
No.   This may not work.  It seems to me  this would crash the GUI layout. because you are not putting Projects and Subprojects in proper places.

For your convenience , I'm attaching  the GUI  and its relevant background HTML . Please  see the GUI layout  and the  relevant background html attached . I wish to populate this layout with my Project VO's


GUI
 subproject.png

 
<tr>
	<td colspan="2" class="table_right_border">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td width="72%" valign="middle" class="table_header_bg">Name of
			Project</td>
			<td width="24%" valign="middle" class="table_header_bg">Subproject</td>
		</tr>
		<tr>
			<td rowspan="3" valign="middle" class="table_base_bg">Project1</td>
			<td valign="middle" class="table_base_bg">1. Subproject1</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">2. Subproject2</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">3. Subproject3</td>
		</tr>
		<tr>
			<td rowspan="3" valign="middle" class="table_base_bg">Project2</td>
			<td valign="middle" class="table_base_bg">1. Subproject1</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">2. Subproject2</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">3. Subproject3</td>
		</tr>
	</table>
	</td>
</tr>

Open in new window

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34988654
>>>It seems to me  this would crash the GUI layout

I can't figure out why do you say that. The logic is good, you only have to replace the JSTL tags with the appropriate Struts2 tags. At least, did you give it a go?
 
<tr>
      <td colspan="2" class="table_right_border">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <c:forEach var="project" items=${projects}>
                    <tr>
                             <td valign="middle" class="table_base_bg">${project.name}</td>
                             <td>
                                     <table width="100%">
                                             <c:forEach var="subproject" items="${project.subprojects}" varStatus="index">
                                                       <tr>
                                                                <td>${index + 1}. ${subproject}</td>
                                                       </tr>
                                             </c:forEach>                        
                                     </table>
                             </td>
                    </tr>
                </c:forEach>
      </table>
      </td>
</tr>
0
 

Author Comment

by:cofactor
ID: 34988718
Where is the header "Name of the Project"  and  "Subproject"  in your code  ?  In the HTML I had these headers.
0
 
LVL 9

Expert Comment

by:ioanton
ID: 34988737
Oh, is that a problem? Here they are:

<tr>
      <td colspan="2" class="table_right_border">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                       <th>Name of the Project</th>
                       <th>Subproject</th>
                <tr>
                <c:forEach var="project" items=${projects}>
                    <tr>
                             <td valign="middle" class="table_base_bg">${project.name}</td>
                             <td>
                                     <table width="100%">
                                             <c:forEach var="subproject" items="${project.subprojects}" varStatus="index">
                                                       <tr>
                                                                <td>${index + 1}. ${subproject}</td>
                                                       </tr>
                                             </c:forEach>                        
                                     </table>
                             </td>
                    </tr>
                </c:forEach>
      </table>
      </td>
</tr>
0
 
LVL 9

Accepted Solution

by:
ioanton earned 800 total points
ID: 34988741
Sorry, there is a typo on the line 7... please put </tr> instead of <tr>
0
 

Author Comment

by:cofactor
ID: 34991455
This works. Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

972 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