需要从数据库中检索行

时间:2019-04-24 19:40:51

标签: jsp servlets

我正在尝试从数据库中逐行检索。在这里,我有2个用户输入,并在查询的“ where”条件下使用这2个输入。但是我的代码无法正常工作。有人,请帮助我!

我正在使用eclipse氧气和mysql工作台

Statements.java:

public class Statements {
    private int accountNo;
    private Date date;
    private String type;
    private double amount;

    public int getAccountNo() {
        return accountNo;
    }
    public void setAccountNo(int accountNo) {
        this.accountNo = accountNo;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }


}

StatementsServlet.java:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        response.setContentType("text/html");

        int accno = Integer.parseInt(request.getParameter("accNo"));
        int date = Integer.parseInt(request.getParameter("month"));

        try {
            Connection con = DBConnection.getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from transactions where accountNo="+accno+" and month(tdate)= "+date+"");

            ArrayList<Statements> list = new ArrayList<>();

            if(rs.next()) {
                Statements sts = new Statements();

                sts.setAccountNo(rs.getInt("accountNo"));
                sts.setDate(rs.getDate("tdate"));
                sts.setType(rs.getString("transType"));
                sts.setAmount(rs.getDouble("amount"));

                list.add(sts);
            }
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayState.jsp");
                request.setAttribute("stlist", list);
                dispatcher.forward(request, response);

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

displayState.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "com.oop.model.Statements" %>
<%@ page import = "java.util.ArrayList" %>
<%@ page import = "java.util.List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<table cellspacing="2" cellpadding="2">
    <tr>
        <th>Date</th>
        <th>Account Number</th>
        <th>Transaction Type</th>
        <th>Amount</th>
    </tr>

<%
    ArrayList<Statements> list = (ArrayList<Statements>) request.getAttribute("stlist");


    for(Statements l:list){%>
        <tr>
            <td>${l.date }</td>
            <td>${l.accountNo }</td>
            <td>${l.type }</td>
            <td>${l.amount }</td>
        </tr>

    <%} %>
</table>
</body>
</html>

它给了我

HTTP状态404-/ Login / StatementsServlet

类型状态报告

消息/ Login / StatementsServlet

说明所请求的资源不可用。

Apache Tomcat / 8.0.36

0 个答案:

没有答案