检索数据并存储在三个不同的对象中,然后存储到arraylist

时间:2016-08-25 13:28:23

标签: java servlets

你好我想从数据库中检索数据,并将存储在三个不同的对象中,我想将这些对象存储到arraylist中,并再次想要将三个对象数据显示为jsp作为三种类型的数据。我可以做对于一个对象,但我该如何处理这三个对象。任何想法?

这是我的servlet代码

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);
        }
    }
}

接下来是接口类和实现接口类

的其他类
* @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);
}

这个类正在实现人员界面

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;
    }
}

2 个答案:

答案 0 :(得分:2)

假设您使用group by语句计算三个不同表的平均值,该表返回多行。   - 拳头我从不同的表中写了三个不同的查询   - 第二我创建了三个Arraylist对象(firstArr,secondArr,thirdArr)来存储结果集   - 第三,我创造了一个更多的集体对象和#34;添加三个对象&#34;进入这个finalArr对象。

最后,您将拥有一个包含所有结果集数据的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;
}

答案 1 :(得分:0)

迭代你的三个物体。在你的情况下,一个聪明的方法是:

@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);
    }     
}

您必须创建由StaffCasualStaffFullTimeStaff实施的Java接口PartTimeStaff才能执行此代码。名字和姓氏必须在界面中。它也会更聪明;)

希望能帮到你