-1
 I have a jsp page, a javabean and a servlet connecting to a database. 

我希望在 jsp 页面中以表格的形式打印满足特定查询的所有行。
当我执行代码时,我只得到满足条件的最后一行。我猜前面的所有行都被覆盖了。如何以表格格式打印所有必需的行?我是java新手,所以我不熟悉所有功能。我写的代码如下。

JSP Page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="ASB" class="project.bank.web.AccountSummaryBean" scope="session" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Account Summary</title>
</head>
<body>
<form action="AccountSummary" method="post">
<table>
            <tr>
                <td>Customer ID: <%=request.getSession().getAttribute("custID_user") %></td>
                <th><jsp:setProperty property="custID" name="ASB" value="<%=request.getSession().getAttribute(\"custID_user\") %>"/>
            </tr>
</table>

-------------------------------------------------------------------------------
Servlet:
-------------------------------------------------------------------------------

package project.bank.web;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AccountSummary extends HttpServlet {

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    AccountSummaryBean asb = null;

    public AccountSummary(AccountSummaryBean asb){
        this();
        this.asb = asb;
    }
    public AccountSummary() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("i am here");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/bankdb", "root", "root");

            pst = conn
                    .prepareStatement("select * from custacc where custID=(?)");

        } catch (ClassNotFoundException e) {
            System.out.println("Driver class not found in the classpath");
        } catch (SQLException e) {
            System.out.println(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String custID = (String) req.getSession().getAttribute("custID_user");

        try {
            int count = 0;
            pst.setString(1, custID);
            rs = pst.executeQuery();
            HttpSession session = req.getSession(false);
            while (rs.next()) {

                session.setAttribute("accNo", rs.getString("accNo"));


                count++;

            }
            RequestDispatcher rd = req
                    .getRequestDispatcher("/AccountSummaryPageScreen.jsp");
            rd.forward(req, resp);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}

-------------------------------------------------------------------------------------
Java Bean
-------------------------------------------------------------------------------------
package project.bank.web;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class AccountSummaryBean{


        String custID;
        String accNo;
        String branch;
        String balance;
        int count=0;

        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pst = null;



        public AccountSummaryBean() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("i am here");
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/bankdb", "root", "root");

                pst = conn
                        .prepareStatement("select * from custacc where custID=(?)");

            } catch (ClassNotFoundException e) {
                System.out.println("Driver class not found in the classpath");
            } catch (SQLException e) {
                System.out.println(e);
            }
        }


        public void display(){
            String custID = getCustID();
            try{
                pst.setString(1, custID);
                rs = pst.executeQuery();
                //HttpSession session = req.getSession(false);
                while (rs.next()) {
                    rs.next();
                    //session.setAttribute("accNo", rs.getString("accNo"));
                    count++;
                    setAccNo(rs.getString("accNo"));
                    setBranch(rs.getString("branch"));
                    setBalance(rs.getString("balance"));
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

            public String getCustID() {
                return custID;
            }

            public void setCustID(String custID) {

                this.custID = custID;
                System.out.println("yoyo its working");
                System.out.println("cust id is "+getCustID());
                display();
            }

            public String getAccNo() {
                return accNo;
                //return "aabbccdd";
            }

            public void setAccNo(String accNo) {
                this.accNo = accNo;
            }

            public String getBranch() {
                return branch;
            }

            public void setBranch(String branch) {
                this.branch = branch;
            }

            public String getBalance() {
                return balance;
            }

            public void setBalance(String balance) {
                this.balance = balance;
            }

}




<table border="2" bgcolor="pink" width=300 height=50 align=center>
  <tr>
    <th>Account Number</th>
    <th>Branch</th> 
    <th>Balance</th>    
  </tr>

  <tr>
    <th><jsp:getProperty property="accNo" name="ASB"/>
    <th><jsp:getProperty property="branch" name="ASB"/></th> 
    <th><jsp:getProperty property="balance" name="ASB"/></th> 
  </tr>

</table>
<div align="center">
    <br>
    <a href="GenerateStatementScreen.jsp">Generate statement</a><br><br>
    <a href="FundTransferHomepageScreen.jsp">Transfer Funds</a><br><br>
    <a href="CheckCustID.jsp">Login Screen</a>
</div>
4

1 回答 1

1

该方法非常简单。为您的数据库连接创建一个 Java 类并将记录列表返回给 JSP。将列表返回给 JSP 后,您必须遍历列表并显示这些记录。查看以下链接以了解如何实现此目的:如何通过 java 类在 jsp 中显示值

于 2013-08-27T04:22:39.610 回答