How can I display multiple records from query

I can only display one record from sql to jsp so far

Page 1 of 1

4 Replies - 1577 Views - Last Post: 02 March 2010 - 11:36 AM Rate Topic: ***** 1 Votes

#1 bigz008  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 38
  • Joined: 17-September 09

How can I display multiple records from query

Posted 02 March 2010 - 08:31 AM

Hi, I'm creating a page in which an admin can add multiple records in one table. However, I cannot display multiple records because when I add a record from SQL, it just replaces my other records instead of adding a new row:

FULL CODE:
<% 
           String skuResult[][];
           skuResult = null;
        try {
   
String []  skuFields = {"orgID", "length", "amount", "trial_period", "promoMins"};
CRUD skuCRUD = new CRUD ("sku", "skuID");  
     skuResult = skuCRUD.getRecords(skuFields,0,0);

        } catch (Exception e) { 
Tracer.logERROR("[sku/skuContent.jsp] "+e);
        e.printStackTrace();            }             

    String    skuID="";  
    String    organisationID="";  
    String    cc_flag="";       
    String    length="";  
    String    amount="";  
    String    active="";  
    String    trialPeriod="";  
    String    description="";  
    String    promoText="";  
    String    promoMinutes="";  


for (int i = 0; i < skuResult.length; i++) {
organisationID = skuResult[i][0]; 
length = skuResult[i][1] ;
amount = skuResult[i][2];
trialPeriod = skuResult[i][3];
promoMinutes = skuResult[i][4];
%> 

<%     Tracer.logERROR("[sku/skContent.jsp] "+"errors are after this"); } %>

<table style="width: 99%">
<tr>
<td style="width: 85px;" id="tableheadercell">Organsation ID</td>
<td style="width: 50px;" id="tableheadercell">Length</td>
<td style="width: 50px;" id="tableheadercell">Amount</td>
<td style="width: 80px;" id="tableheadercell">Trial Period</td>
<td style="width: 100px;" id="tableheadercell">Promo Minutes </td>                              
</tr>

<tr>
<td style="width: 85px"><b><%= organisationID %></b></td>
<td style="width: 50px"><b><%= length %></b></td>
<td style="width: 50px"><b><%= amount %></b></td>
<td style="width: 80px"><b><%= trialPeriod %></b></td>
<td style="width: 100px"><b><%= promoMinutes %></b></td>
</tr>
</table>                                
</html>




Basically, what I want is to be able to display multiple records in the database in multiple rows. So far I can only display one:

Attached Image

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: How can I display multiple records from query

#2 egof  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 132
  • Joined: 27-March 09

Re: How can I display multiple records from query

Posted 02 March 2010 - 08:44 AM

You need to move the html table portions around. Move the table and the first row to the top then shove your data row into your loop.

I havent tried this code but this should give you an idea
<table style="width: 99%"> 
<tr> 
<td style="width: 85px;" id="tableheadercell">Organsation ID</td> 
<td style="width: 50px;" id="tableheadercell">Length</td> 
<td style="width: 50px;" id="tableheadercell">Amount</td> 
<td style="width: 80px;" id="tableheadercell">Trial Period</td> 
<td style="width: 100px;" id="tableheadercell">Promo Minutes </td>                               
</tr> 

<%  
           String skuResult[][]; 
           skuResult = null; 
        try { 
    
String []  skuFields = {"orgID", "length", "amount", "trial_period", "promoMins"}; 
CRUD skuCRUD = new CRUD ("sku", "skuID");   
     skuResult = skuCRUD.getRecords(skuFields,0,0); 
 
        } catch (Exception e) {  
Tracer.logERROR("[sku/skuContent.jsp] "+e); 
        e.printStackTrace();            }              
 
    String    skuID="";   
    String    organisationID="";   
    String    cc_flag="";        
    String    length="";   
    String    amount="";   
    String    active="";   
    String    trialPeriod="";   
    String    description="";   
    String    promoText="";   
    String    promoMinutes="";   
 
 
for (int i = 0; i < skuResult.length; i++) { 
organisationID = skuResult[i][0];  
length = skuResult[i][1] ; 
amount = skuResult[i][2]; 
trialPeriod = skuResult[i][3]; 
promoMinutes = skuResult[i][4]; 
%>  
 <tr> 
<td style="width: 85px"><b><%= organisationID %></b></td> 
<td style="width: 50px"><b><%= length %></b></td> 
<td style="width: 50px"><b><%= amount %></b></td> 
<td style="width: 80px"><b><%= trialPeriod %></b></td> 
<td style="width: 100px"><b><%= promoMinutes %></b></td> 
</tr> 

<%     Tracer.logERROR("[sku/skContent.jsp] "+"errors are after this"); } %> 
 
</table>                                 
</html>


Was This Post Helpful? 0
  • +
  • -

#3 bigz008  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 38
  • Joined: 17-September 09

Re: How can I display multiple records from query

Posted 02 March 2010 - 09:09 AM

Honestly, Thank you! This worked like a charm! So exactly what is the process here? I'm sort of a noob so I was curious what happened when we put it in the loop?

Thanks once again! :)
Was This Post Helpful? 0
  • +
  • -

#4 egof  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 132
  • Joined: 27-March 09

Re: How can I display multiple records from query

Posted 02 March 2010 - 10:39 AM

you were looping all of the data items and putting them in a variable which is good, but then you got out of the loop and printed the last item placed in the variables. Moving the html portion in the looped allowed it to display all of the items.
Was This Post Helpful? 0
  • +
  • -

#5 bigz008  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 38
  • Joined: 17-September 09

Re: How can I display multiple records from query

Posted 02 March 2010 - 11:36 AM

Ok Thanks.

View Postegof, on 02 March 2010 - 09:39 AM, said:

you were looping all of the data items and putting them in a variable which is good, but then you got out of the loop and printed the last item placed in the variables. Moving the html portion in the looped allowed it to display all of the items.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1