Improve company productivity with a Business Account.Sign Up

x
?
Solved

populate records into VO

Posted on 2011-02-24
17
Medium Priority
?
315 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
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.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

585 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