Solved

populate records into VO

Posted on 2011-02-24
17
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

690 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