gpt4 book ai didi

java - 使用JSTL动态数据库表展示

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:53 24 4
gpt4 key购买 nike

<分区>

我正在尝试像这样使用 JSTL 显示数据库数据:

我的道;

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;
}

我的小服务程序:

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);
}
}

我的 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>

我的显示:

enter image description here

名字和中间名都在同一列上。使用 JSTL 显示动态表的最佳方式是什么。一个例子也将不胜感激。

编辑:我的预期输出是:我添加了一个额外的一列: enter image description here

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com