title: java servlet : how to speed this up?
body: <p>I have the following function which is called for every line item produced.  Does anyone have any ideas how to speed this up?  </p>
  
  <pre><code>private String getDetails(String doc){
      String table="";
      java.sql.ResultSet rs = qw.DBquery("select " +
          "id,LineType, QtyTotal, ManufacturerPartNumber, Description, UnitCost,UnitPrice " +
          "From DocumentItems  " +
          "where DocID="+doc+" order by linenumber " +
          "");
      table+= "&lt;table class=inner&gt;&lt;thead&gt;&lt;colgroup&gt;&lt;col id='col1'&gt;&lt;col id='col2'&gt;&lt;col id='col3'&gt;&lt;col id='col4'&gt;&lt;col id='col5'&gt;&lt;/colgroup&gt;" +
              "&lt;tr class='enetBlue'&gt;&lt;th&gt;Qty&lt;/th&gt;&lt;th&gt;Part Num&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Unit Cost&lt;/th&gt;&lt;th&gt;Unit Price&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;" +
              "&lt;tbody&gt;";
      try{            
          int odd = 0;
          while(rs.next()){
  
              int lineType = rs.getInt("LineType");
              int qty = rs.getInt("QtyTotal");
              String part = rs.getString("ManufacturerPartNumber");
              String desc = rs.getString("Description");
              float cost = rs.getFloat("UnitCost");
              float price = rs.getFloat("UnitPrice");
              String id = rs.getString("id");
  
              String clas="";
  
             if (odd==0) odd=1; else odd=0;
  
             clas="red";
             if (lineType==2) clas="yellow";
             if (lineType==3) clas="yellow";
             if (lineType==4) clas="yellow";
             if (qty==0) clas="yellow";
             java.sql.ResultSet rs2 = mas.DBquery("select itemkey from timitem where itemid = '"+part+"'"); 
             while (rs2.next())
             {
                 if (odd==1) clas="odd";
                 if (odd==0) clas="even";
             }
             table+="&lt;tr class='"+clas+"'&gt;&lt;td&gt;"+qty+"&lt;/td&gt;\n"+
                          "&lt;td&gt;"+part+"&lt;/td&gt;\n"+
                          "&lt;td&gt;"+desc+"&lt;/td&gt;\n"+
                          "&lt;td&gt;"+cost+"&lt;/td&gt;\n"+
                          "&lt;td&gt;"+price+"&lt;/td&gt;&lt;/tr&gt;\n";
  
                         //if clas=red | means item is not found in MAS, gear for insert. 
                         if (clas=="red") { 
  
                          table+="&lt;tr &gt;&lt;td colspan=5&gt;&lt;table border=1&gt;&lt;tr&gt;&lt;td colspan=2&gt;\n";
                          //get unit measure key  
                          try {
                              table+="&lt;form name=masinsert"+id+" method=get action=MASInsert&gt;\n";
  
                      table+="&lt;input type=hidden name=\"partnumber"+id+"\" value=\""+part+"\"&gt;\n";
                      table+="&lt;input type=hidden name=\"itemcost"+id+"\" value=\""+cost+"\"&gt;\n";
                      table+="&lt;input type=hidden name=\"itemlistprice"+id+"\" value=\""+price+"\"&gt;\n";
                      table+="&lt;input type=hidden name=\"itemdescription"+id+"\" value=\""+desc+"\"&gt;\n";
                      table+="&lt;/td&gt;&lt;tr&gt;\n";
  
                              java.sql.ResultSet rsUM = mas.DBquery("select * from tciUnitMeasure where companyid like 'ENS' ");
                                  table+="&lt;tr bgcolor=#990033&gt;&lt;td align=left valign=top&gt;Unit Measure&lt;/td&gt;&lt;td align=left valign=top&gt;&lt;select name=\"UnitMeasKey\"&gt;";
                                          while(rsUM.next())
                                  {
                                      table+="&lt;option value=\"" + rsUM.getString("UnitMeasKey") + "\"&gt;" + rsUM.getString("UnitMeasID") + "&lt;/option&gt;\n"; 
                              }//end while rs1 
                              table+="&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;\n"; 
  
  
                      //build ItemClass options from mas: Puchase ProductLine 
                          java.sql.ResultSet rsPP = mas.DBquery("select * from timPurchProdLine where companyID = 'ENS'");
                          int k = 0; 
  
                          table+= "&lt;tr bgcolor=#990033&gt;&lt;td align=left valign=top&gt;Purchase Product Line&lt;/td&gt;&lt;td align=left valign=top&gt;&lt;select name=\"PurchProdLine\"&gt;\n"; 
                          while(rsPP.next())
                          {
                              table+="&lt;option value=\"" + rsPP.getString("PurchProdLineKey") + "\"&gt;" + rsPP.getString("Description") + "&lt;/option&gt;\n"; 
  
                          }//end while rsPP 
                          table+="&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;\n"; 
  
                          //build item classkey options
                          java.sql.ResultSet rsIC = mas.DBquery("select * from timItemClass where companyID = 'ENS' order by itemclassname desc");
  
                          table+= "&lt;tr bgcolor=#990033&gt;&lt;td align=left valign=top&gt;Item Class :&lt;/td&gt;&lt;td align=left valign=top&gt;&lt;select name=\"itemclasskey\"&gt;\n"; 
                          while(rsIC.next())
                          {
                              table+="&lt;option value=\"" + rsIC.getString("itemclasskey") + "\"&gt;" + rsIC.getString("ItemClassName") + "&lt;/option&gt;\n"; 
  
                          }//end while rs1 
                          table+="&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;";
                          table+="&lt;tr&gt;&lt;td colspan=2&gt;&lt;input id='m"+id+"' type=\"button\" onclick=\"masinsert('"+ id +"')\" value=\"Add to MAS\"&gt;&lt;/td&gt;&lt;/tr&gt;";
                          table+="&lt;/table&gt;\n"; 
  
                  }catch(Exception e){}   //end try
  
                      table+="&lt;/form&gt;\n"; 
                          table+="&lt;/td&gt;&lt;/tr&gt;";
  
  
                  }//end if clas=red
              }//end while 
      }catch(java.sql.SQLException e){
          e.printStackTrace();}        
      table+="&lt;/tbody&gt;&lt;/table&gt;";
      return table;
  }
  </code></pre>
  
  <p>thanks in advance</p>