title: JSP for loop with database results
body: <p>Im using JSP for an application for college. Corresponding with the JSP there is a connection to a database which i must retrieve the information and display it in a drop down list so the user can select one option to proceed with a registration form. I am able to connect to the DB no problem and output the information in the drop down but its only getting the very first row that was entered into the database.</p>
  
  <pre><code>String[] course_codes = dbclass.SelectRow("SELECT DISTINCT Course_code FROM Stream ;");
                  %&gt;&lt;form action="successtwo.jsp"&gt;
                      &lt;label&gt;Select Course code&lt;/label&gt; 
                      &lt;select name="stream"&gt;
                           &lt;%for(String course_code: course_codes){
                           out.println("&lt;option value="+course_code+"&gt;"+course_code+"&lt;/option&gt;");
  }%&gt;
                     &lt;/select&gt;&lt;br&gt;
  </code></pre>
  
  <p>So I have done something like this but I also have done this.</p>
  
  <pre><code>String[] course_codes = dbclass.SelectRow("SELECT DISTINCT Course_code FROM Stream ;");
                  %&gt;&lt;form action="successtwo.jsp"&gt;
                      &lt;label&gt;Select Course code&lt;/label&gt; 
                      &lt;select name="stream"&gt;
                           &lt;%for(String course_code: course_codes){%&gt;
  
                         &lt;option value="&lt;%=course_code%&gt;"&gt;&lt;%=course_code%&gt;&lt;/option&gt; 
  
                             &lt;%}%&gt;
                     &lt;/select&gt;&lt;br&gt;
  </code></pre>
  
  <p>In the database there is more than one Course_code and also the same Course_code can appear hence why i use distinct. Here is the dbClass.java where the SelectRow method is.</p>
  
  <pre><code> public String[] SelectRow(String SQLquery)
  {
      String Result[];
      // Send an SQL query to a database and return the *single column* result in an array of strings
      try {// Make connection to database
          statementObject = connectionObject.createStatement();
  
          ResultSet statementResult = statementObject.executeQuery(SQLquery); //Should connection be left open?
  
          ResultSetMetaData rsmd = statementResult.getMetaData();
          int nrOfColumns = rsmd.getColumnCount();
  
          Result = new String[nrOfColumns];
  
          statementResult.next();
  
          int currentCounter = 0;
  
          while (currentCounter&lt;nrOfColumns) // While there are rows to process
          {
              // Get the first cell in the current row
              Result[currentCounter] = statementResult.getString(currentCounter+1);
              currentCounter++;
  
          }
          // Close the link to the database when finished
  
      } catch (Exception e) {
          System.err.println("Select problems with SQL " + SQLquery);
          System.err.println("Select problem is " + e.getMessage());
          Result = new String[0]; //Need to setup result array to avoid initialisation error
          writeLogSQL(SQLquery + " caused error " + e.getMessage());
          }
      writeLogSQL(SQLquery + "worked ");
      return Result;
  } // End SelectRow
  </code></pre>
  
  <p>Any ideas?</p>