Issue
I have been able to properly display an arraylist that displays all data in my MySQL database, however I'm having trouble how to display data based on userid.
Here is my code:
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);
}
}
Any help or direction will be greatly appreciated. Thanks in advance
EDIT: 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;
}
Solution
Way simpler than you think:
First, fix the DAO and use the parameter. Changes below:
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
...
}
Then you can simply use the resulting list in the 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");
}
This code:
- Retrieves the parameter
id
(filtering condition). - Retrieves the credits for the specific user.
- It then places them in the attribute
creditlist
of the request scope. - It finally redirects to the new resulting page, that will show the credits.
Answered By - The Impaler