MySql表 - 显示jsp只显示第一行

时间:2018-04-19 16:26:23

标签: mysql jsp

我试图将MySql数据库中的所有记录显示到JSP页面中。 我有以下代码,但它只显示第一行。如何让表格显示所有行?

请注意我是初学者,所以不清楚这应该如何运作......

try{  
                  String myDriver = "org.gjt.mm.mysql.Driver";//db driver 
                  String myUrl = database;//connect to db
                  Class.forName(myDriver);
                  Connection conn = DriverManager.getConnection(myUrl, user, password);//authenticating on database
                  PreparedStatement sta = conn.prepareStatement("SELECT * FROM customerdb");//prepared statement 

                  ResultSet rst = sta.executeQuery();


         while (rst.next()){


            String cusid = rst.getString("Cus_ID"); //variable to retrieve the customer from the database
            String cusn = rst.getString("Cus_name");
            String cussn = rst.getString("Cus_surname");
            String cusm = rst.getString("Cus_mail");
            String cusaddr = rst.getString("Cus_Address");
            String custel = rst.getString("Cus_telephone");

       if(rst.next()){
          request.setAttribute("ID", cusid);
          request.setAttribute("name", cusn);
          request.setAttribute("surname", cussn);
          request.setAttribute("mail", cusm);
          request.setAttribute("address", cusaddr);
          request.setAttribute("telephone", custel); 


          request.getRequestDispatcher("ListAllCustomer.jsp").forward(request, response); 
       }

1 个答案:

答案 0 :(得分:0)

你的jsp页面只显示第一行的原因是:
获取第一条记录后,转到ListAllCustomer.jsp页面,然后其他行无法设置

if(rst.next()){
    request.setAttribute("ID", cusid);
    request.setAttribute("name", cusn);
    request.setAttribute("surname", cussn);
    request.setAttribute("mail", cusm);
    request.setAttribute("address", cusaddr);
    request.setAttribute("telephone", custel); 

    //this line make it only shows the first record
    request.getRequestDispatcher("ListAllCustomer.jsp").forward(request, response);
}

此外,您需要避免在获取数据库记录时使用rst.next(),因为每次调用它时,它都会移动到下一条记录,因此您将丢失一些记录。

要显示所有记录的解决方案,您需要重新设计代码,我的建议如下所示:

首先创建一个模型调用User来存储信息:

public class User {

    private String id;
    private String name;
    private String surname;
    private String mail;
    private String address;
    private String telephone;

    public User(){
    }

    public User(String id, String name, String surname, String mail,
            String address, String telephone) {
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.mail = mail;
        this.address = address;
        this.telephone = telephone;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
}

然后像下面那样更改你的jsp代码来获取和存储数据:

List<User> userList = new ArrayList<User>();
while (rst.next()){
    String cusid = rst.getString("Cus_ID"); //variable to retrieve the customer from the database
    String cusn = rst.getString("Cus_name");
    String cussn = rst.getString("Cus_surname");
    String cusm = rst.getString("Cus_mail");
    String cusaddr = rst.getString("Cus_Address");
    String custel = rst.getString("Cus_telephone");

    User user = new User(cusid,cusn,cussn,cusm,cusaddr,custel);
    userList.add(user);
   }

request.setAttribute("userList",userList);
request.getRequestDispatcher("ListAllCustomer.jsp").forward(request, response);

最后,您可以使用ListAllCustomer.jspJSTLEL Expression中显示记录,如下所示:

<table>
<tbody>
<c:forEach items="${userList}" var="user">
  <tr>
   <td>id:</td> <td>${user.id} </td>
   <td>name:</td> <td>${user.name} </td>
   <td>surname:</td> <td>${user.surname} </td>
   <td>mail:</td> <td>${user.mail} </td>
   <td>address:</td> <td>${user.address} </td>
   <td>telephone:</td> <td>${user.telephone} </td>
  </tr>
</c:forEach>
</tbody>
</table>