2016-08-25 11 views
-2

こんにちは私はデータベースからデータを取得したいと思う3つの異なるオブジェクトに格納され、それらのオブジェクトをarraylistに格納し、 3種類のデータとしてjspに変換することができます.1つのオブジェクトに対して行うことはできますが、どのように3つのオブジェクトに対して行うことができますか。データを取得して3つの異なるオブジェクトに格納してからarraylistに返す

が、これはこの以下の私のサーブレットコード

public class ViewCasualStaff extends HttpServlet { 

    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException{ 

     try { 
      PrintWriter out =response.getWriter(); 
      response.setContentType("text/html"); 

      Connection conn = ConnectionManager.getConnection(); 
      PreparedStatement pst = conn.prepareStatement(""); 
      String sql = "SELECT LAST_NAME,FIRST_NAME FROM EMPLOYEE " 
        + "WHERE JOB_TYPE = casual"; 

      pst.executeQuery(sql); 
      ResultSet rs = pst.getResultSet(); 

      ArrayList<CasualStaff> casualStafflist = new ArrayList<>(); 

      while(rs.next()){ 

       CasualStaff cs = new CasualStaff(); 
       cs.setFname(rs.getString("FIRST_NAME")); 
       cs.setLname(rs.getString("LAST_NAME")); 
       casualStafflist.add(cs); 

      } 

      request.setAttribute("CSList",casualStafflist); 

      RequestDispatcher dispatcher = request.getRequestDispatcher("/test.jsp"); 

      dispatcher.forward(request,response); 
     } catch (InstantiationException | IllegalAccessException | SQLException ex) { 
      Logger.getLogger(ViewCasualStaff.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

あるインタフェースクラスこのクラスはオーバースタッフインターフェイスに

public class CasualStaff implements Staff{ 

    private String Fname; 
    private String Lname; 
    private String Position; 

    @Override 
    public void setStaff(String Fname, String Lname, String Position) { 
     this.Fname= Fname; 
     this.Lname=Lname; 
     this.Position = Position; 
    } 
} 
+0

に結果を表示することができ、私はなぜあなたは同じプロセスに従うことができない、という問題が表示されないのですか? –

+0

私は新しいarraylistを作成して、2番目のオブジェクトを追加しなければならないということを意味します。 – Dipen

+0

はい、3つのarraylistを 'request'属性として設定します。 –

答えて

0

反復を実施している

* @author dipendra 
*/ 

public interface Staff { 

    public String FName= "Fname"; 
    public String LName ="Lname"; 
    public String Postion = "Position"; 

    public void setStaff(String Fname,String Lname,String Position); 
} 

を実装インタフェースクラスと他のクラスあなたの3つのオブジェクト。あなたのケースでスマートな方法は、次のようになります。

@Override 
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException{ 

    try { 
     PrintWriter out =response.getWriter(); 
     response.setContentType("text/html"); 

     Connection conn = ConnectionManager.getConnection(); 
     PreparedStatement pst = conn.prepareStatement(""); 

     String[] jobTypes = {"casual", "full time", "part time"}; 
     ArrayList<Staff>[] myData = new ArrayList<>()[jobTypes.length]; 

     for (int i = 0; i < jobTypes.length; i++) { 
      String sql = "SELECT LAST_NAME,FIRST_NAME FROM EMPLOYEE " 
        + "WHERE JOB_TYPE = " + jobTypes[i]; 

      pst.executeQuery(sql); 
      ResultSet rs = pst.getResultSet(); 

      ArrayList<Staff> staffList= new ArrayList<>(); 

      while(rs.next()) { 

       Staff cs = new Staff(); 
       cs.setFname(rs.getString("FIRST_NAME")); 
       cs.setLname(rs.getString("LAST_NAME")); 
       staffList.add(cs); 

      } 
      myData[i] = staffList; 
     } 

     request.setAttribute("SList", myData);  

     RequestDispatcher dispatcher = request.getRequestDispatcher("/test.jsp"); 

     dispatcher.forward(request,response); 
    } catch (InstantiationException | IllegalAccessException | SQLException ex) { 
     Logger.getLogger(ViewCasualStaff.class.getName()).log(Level.SEVERE, null, ex); 
    }  
} 

あなたが可能なこのコードを実行するためにCasualStaffFullTimeStaffPartTimeStaffによって実装されるJavaインタフェースStaffを作成する必要があります。姓と名がインターフェイスに必要です。 )あなたは

+0

ここで何をしましたか? – Dipen

+0

@Dipenコンパクトな方法で3つのオブジェクトに対して同じプロセスを実行しました。 –

+0

* @author dipendra */ パブリックインターフェイススタッフ{ パブリック文字列ます。FName = "FNAME"。 public String LName = "Lname"; public String Postion = "Position"; パブリックvoid setStaff(文字列Fname、文字列Lname、文字列の位置); } – Dipen

2

あなたが複数の行を返す文でグループを使用して、3つの異なるテーブルから平均値を計算していると仮定助けに

希望、それはまた、よりスマートになります。 - Fist私は別のテーブルから3つの異なるクエリを書いた - 2番目私は結果セットを格納する3つのArraylistオブジェクト(firstArr、secondArr、thirdArr)を作成しました - 私はもう一つのarralistオブジェクトを作成し、オブジェクト。

最後に、すべての結果セットデータを含むfinalArrオブジェクトがあります。

今、あなたはfinalArrを反復処理して、JSPや希望ビュー

public ArrayList getAverage() { 
    // TODO Auto-generated method stub`enter code here` 
    String query1= "select sum(column)/count(Column) as avr1 from table1 group by(column1) order by desc"; 
    String query2= "Select sum(column)/count(Column) as avr2 from table2 group by(column1) order by desc"; 
    String query3= "select sum(column)/count(Column) as avr3 from table3 group by(column1) order by desc";  

    Connection connection = null; 
    ResultSet rs = null; 
    PreparedStatement preparedStatement = null; 
    ArrayList fisrtArr = new ArrayList(); 
    ArrayList secondArr = new ArrayList(); 
    ArrayList thirdArr = new ArrayList(); 
    float average1= 0; 
    float average2= 0; 
    float average3= 0; 
    try { 
     connection = dataSourceAbacus.getConnection(); 
     preparedStatement = connection.prepareStatement(query1); 
     rs = preparedStatement.executeQuery(); 
    while (rs.next()) { 
      average1= rs.getInt("avr1"); 
      firstArr.add(average1); 
     } 
     connection.close(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } finally { 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
      } 
     }if (rs != null){ 
      try{rs.close(); 
      }catch (Exception e){e.printStackTrace(); 
      } 
     }if(preparedStatement != null){ 
      try{preparedStatement.close(); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 


    try { 
     connection = dataSourceAbacus.getConnection(); 
     preparedStatement = connection.prepareStatement(query2); 
     rs = preparedStatement.executeQuery(); 
    while (rs.next()) { 
      average2= rs.getInt("avr2"); 
      secondArr.add(average2); 
     } 
     connection.close(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } finally { 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
      } 
     }if (rs != null){ 
      try{rs.close(); 
      }catch (Exception e){e.printStackTrace(); 
      } 
     }if(preparedStatement != null){ 
      try{preparedStatement.close(); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 

    try { 
     connection = dataSourceAbacus.getConnection(); 
     preparedStatement = connection.prepareStatement(query3); 
     rs = preparedStatement.executeQuery(); 
    while (rs.next()) { 
      average3 = rs.getFloat("avr3"); 
      thirdArr.add(average3); 
     } 
     connection.close(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } finally { 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
      } 
     }if (rs != null){ 
      try{rs.close(); 
      }catch (Exception e){e.printStackTrace(); 
      } 
     }if(preparedStatement != null){ 
      try{preparedStatement.close(); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 
    finalArr.add(firstArr); 
    finalArr.add(secondArr); 
    finalArr.add(thirdArr); 
    return finalArr; 
} 
関連する問題