如何正确返回一个基于用户标识显示数据的数组列表?

时间:2019-03-21 14:37:43

标签: java mysql servlets arraylist

我已经能够正确显示一个数组列表,该列表显示MySQL数据库中的所有数据,但是我在如何基于用户ID显示数据时遇到了麻烦。

这是我的代码:

CreditsServlet.java

private void listTrans(HttpServletRequest request, HttpServletResponse response)
        throws SQLException, IOException, ServletException {

    int id = Integer.parseInt(request.getParameter("id"));
    List<Credits> creditlist = OrganizerDao.getAllCredits(id);

    for (Credits credits : creditlist) {
        if (credits.getOrganizerid() == id) {
            request.setAttribute("creditlist", creditlist);
        }

        request.getRequestDispatcher("credits.jsp").include(request, response);
    }
}

任何帮助或指导将不胜感激。预先感谢

编辑:CreditsDao.java

public static List<Credits> getAllCredits(int id) throws SQLException {

    List<Credits> creditlist=new ArrayList<>();

    try{
        Connection con= OrganizerDao.getConnection();
        PreparedStatement ps=con.prepareStatement("select users.id AS organizerid, credits.invoiceid, " +
                "credits.amountin AS creditamount, attendees.name AS payee, credits.purpose, gateway.name AS gatewayname " +
                "from users, credits, attendees, gateway where credits.userid=users.id and credits.attendeeid=attendees.id " +
                "and credits.gateway=gateway.id");

        ResultSet rs=ps.executeQuery();
        while(rs.next()){

            Credits credits = new Credits();

            credits.setOrganizerid(rs.getInt("organizerid"));
            credits.setInvoiceid(rs.getInt("invoiceid"));
            credits.setCreditamount(rs.getDouble("creditamount"));
            credits.setPayee(rs.getString("payee"));
            credits.setPurpose(rs.getString("purpose"));
            credits.setGatewayname(rs.getString("gatewayname"));

            creditlist.add(credits);

        }
        con.close();
    }catch(Exception e){e.printStackTrace();}

    return creditlist;

}

1 个答案:

答案 0 :(得分:1)

比您想象的简单:

首先,修复DAO并使用参数。以下更改:

public static List<Credits> getAllCredits(int id) throws SQLException {

  ...

  PreparedStatement ps=con.prepareStatement("select users.id AS organizerid, credits.invoiceid, " +
    "credits.amountin AS creditamount, attendees.name AS payee, credits.purpose, gateway.name AS gatewayname " +
    "from users, credits, attendees, gateway where credits.userid=users.id and credits.attendeeid=attendees.id " +
    "and credits.gateway=gateway.id" +
    " and users.id = ?"); // Added this extra SQL code
  ps.setInt(1, id); // Added this code to use the id parameter

  ...

}

然后,您可以简单地在servlet中使用结果列表:

private void listTrans(HttpServletRequest request, HttpServletResponse response)
        throws SQLException, IOException, ServletException {
  int id = Integer.parseInt(request.getParameter("id"));
  List<Credits> creditlist = OrganizerDao.getAllCredits(id);
  request.setAttribute("creditlist", creditlist);
  response.sendRedirect("resultpage.jsp");  
}

此代码:

  • 检索参数id(过滤条件)。
  • 检索特定用户的信用。
  • 然后将它们放置在请求范围的属性creditlist中。
  • 它最终重定向到新的结果页面,该页面将显示功劳。