[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

ODBC JSP ( Losing Variables..)

I'm reading from a Database using an ODBC Connection. I'm having trouble looping through the ResultSets without losing variables or it completly just displaying blank. Before I had everything rs.getString(#) inside of the output statements, but that caused problems for some reason and now I had it somewhat workign when i definied variables to the getString() statements. But, now it's just stopping completely and just displaying 1 line of data??
 
---------------------------------JSP CODE HERE------------------------------------------
 String url = "jdbc:odbc:" + serverName;
  // CONNECTION CODE HERE

  ResultSet rs = stmt.executeQuery("BIG QUERY HERE RETURNS 13 COLUMNS")

        
      int loop_amount = 0;
      String img_link = null;
      int p=1;
      
      String Name = null;
      String TREND_Name = null;
      String FOLDER_Name = null;
      String PAGE_URL = null;
      String MAIN_Name = null;
      String output = null;
      String COLOR_Name = null;

      while (rs.next())
      {


      
      loop_amount = rs.getInt(1);
      MAIN_Name = rs.getString(3);
      PAGE_URL = rs.getString(5);
      
      
      
      output = "<li><a href=" + PAGE_URL + "<u>" + MAIN_Name + "</u></font></a><ul>";
      
      out.println(output);
            

      if(loop_amount != 1)
            {
            

                  for(p=1; p <= loop_amount;p++)
                  {
                        COLOR_Name = rs.getString(13);
                        TREND_Name = rs.getString(8);
                        Name = rs.getString(4);

                        
                        out.println("<li><font size=1 face=\"Verdana\" color=\"" + COLOR_Name + "\">" + Name + "</font></li>");
                        
                        
                        if(p!=loop_amount)
                        {
                              rs.next();
                        }
                  
                  }
            
                  
            
            }
            else
            {
                  Name = rs.getString(4);
                  COLOR_Name = rs.getString(13);
                  TREND_Name = rs.getString(8);
                  
                  
                  out.println("<li><font face=\"Verdana\" size=1 color=\"" + COLOR_Name + "\">" + Name + "</font></li>");
            }

      out.println("</li></ul>");      

      }

 --------------------------------END OF JSP CODE----------------------------
0
kak39
Asked:
kak39
2 Solutions
 
runa_paathakCommented:
Are you sure your query is returning more than one row? If you are sure it is returning more than one row, the only reason I can think of is that your loop_amount could be 0. That will mean it will come into the if block but not into the for loop. Print out loop_amount in the if block and find out.
0
 
pankilmpatelCommented:
Try to remove data betweem

ResultSet rs = stmt.executeQuery("BIG QUERY HERE RETURNS 13 COLUMNS")
and
while (rs.next())

and put it before rs declaration

0
 
MeritorCommented:
in this code :


              for(p=1; p <= loop_amount;p++)
               {
                    COLOR_Name = rs.getString(13);
                    TREND_Name = rs.getString(8);
                    Name = rs.getString(4);

                   
                    out.println("<li><font size=1 face=\"Verdana\" color=\"" + COLOR_Name + "\">" + Name + "</font></li>");
                   
                   
                    if(p!=loop_amount)
                    {
                         rs.next();
                    }
               
               }


Yor are reading the data from the recordset more then onetime as per JDBC api's documentation: "For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once."

In my openion please debug this code and your problem may be solved.
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now