Java 使用 Jstl 动态显示数据库表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19090153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:59:59  来源:igfitidea点击:

Dynamic database table display using Jstl

javaservletsdynamic-data-display

提问by ErrorNotFoundException

I am trying to display database data using JSTL like this:

我正在尝试使用 JSTL 显示数据库数据,如下所示:

My dao;

我的道;

public ArrayList getStudentFirstName(){
        ArrayList v = new ArrayList();
        Connection conn;
        try{
         conn =  db.getDbConnection();
         String sql = "select STU_FIRST_NAME, STU_MIDDLE_NAME, LAST_NAME from college_students_master";
         PreparedStatement ps = conn.prepareStatement(sql);
         ResultSet rs = ps.executeQuery();
         while(rs.next()){
             String firstname = rs.getString("STU_FIRST_NAME");
             String middlename = rs.getString("STU_MIDDLE_NAME");
             String lastname = rs.getString("LAST_NAME");
             v.add(firstname);
             v.add(middlename);
             v.add(lastname);
         }
        }catch(Exception asd){
            System.out.println(asd.getMessage());
        }
        return v;
    }

My servlet:

我的 servlet:

public class displayservlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      DetailsDao dd = new DetailsDao();
      request.setAttribute("firstname", dd.getStudentFirstName());
        RequestDispatcher view = request.getRequestDispatcher("DemoJSP.jsp");
        view.forward(request, response);
    }
}

My JSP:

我的 JSP:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    </head>
    <body>
        <b>The Demo Object Names Are:-
            <br>

            <table>
                <c:forEach items="${firstname}" var="firstname">
                    <tr>
                        <td>${firstname}</td>
                    </tr>
                </c:forEach>

            </table>
    </body>
</html>

My Display:

我的显示器:

enter image description here

在此处输入图片说明

Both The first Name and the MiddleName are on the same Column. What is the best way to display a dynamic table using Jstl. an Example would also be Appreciated.

The first Name 和 MiddleName 都在同一列上。使用 Jstl 显示动态表的最佳方式是什么?一个例子也会受到赞赏。

EDIT: My expected Output Is: I have added an extra One column: enter image description here

编辑:我的预期输出是:我添加了一个额外的一列: 在此处输入图片说明

采纳答案by newuser

Try this,

尝试这个,

create a new Bean class named as Name

创建一个名为 Name 的新 Bean 类

public class Name
{
    private String firstName;
    private String middleName;
    private String lastName;

   // relavent getter setter
}

change your method getStudentFirstName()

改变你的方法 getStudentFirstName()

public ArrayList getStudentFirstName(){
        ArrayList<Name> v = new ArrayList<Name>();
        Connection conn;
        try{
         conn =  db.getDbConnection();
         String sql = "select STU_FIRST_NAME, STU_MIDDLE_NAME from college_students_master";
         PreparedStatement ps = conn.prepareStatement(sql);
         ResultSet rs = ps.executeQuery();
         Name name = null;
         while(rs.next()){
             name = new Name();
             name.setFirstName(rs.getString("STU_FIRST_NAME")); //set your firstName
             name.setMiddleName(rs.getString("STU_MIDDLE_NAME")); //set your MiddleName
             name.setLastName(rs.getString("LAST_NAME")); //set your LastName
             v.add(name); 
         }
        }catch(Exception asd){
            System.out.println(asd.getMessage());
        }
        return v;
    }

change your c:forEachLoop

改变你的c:forEach循环

<table border="1">
  <thead>
    <td>
      <th>First Name</th>
      <th>Middle Name</th>
      <th>Last Name</th>
    </td>
  </thead>
  <tbody>
     <c:forEach items="${firstname}" var="name">
       <tr>
         <td>${name.firstName}</td>
         <td>${name.middleName}</td>
         <td>${name.lastName}</td>
       </tr>
     </c:forEach>
  </tbody>
</table>