循环输出

mac2025-02-14  10

使用foreach循环输出List中的数据

步骤详解: 1.数据库的设计和实现 2. Java代码的书写 3. jsp页面的显示

效果图展示

数据库数据 查询结果的显示 功能分析:

链接数据库查找数据库内容把这些内容放入List数组里面用session传递jsp页面获取session内容用foreach循环输出内容

了解一下目录结构吧! ==注意:==写代码之前一定要添加相应的架包

代码展示

数据库链接((util包里的DBUtil.java代码))

package com.hnpi.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConn(){ String url = "jdbc:sqlserver://localhost:1433;databaseName=Manager"; String user = "sa"; String pwd = "1"; Connection conn = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection(url, user, pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConn(Connection conn, PreparedStatement ps, ResultSet rs){ if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

People类的实现((bean包里的People.java代码))

package com.hnpi.bean; public class People { private int id; private String name; private String pwd; private String reallyName; private String branch; private String role; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getReallyName() { return reallyName; } public void setReallyName(String reallyName) { this.reallyName = reallyName; } public String getBranch() { return branch; } public void setBranch(String branch) { this.branch = branch; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public People() { super(); // TODO Auto-generated constructor stub } public People(int id, String name, String pwd, String reallyName, String branch, String role) { super(); this.id = id; this.name = name; this.pwd = pwd; this.reallyName = reallyName; this.branch = branch; this.role = role; } }

查询内容并用session传输((servlet包里的UserSelectServlet.java代码))

package com.hnpi.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.hnpi.bean.People; import com.hnpi.util.DBUtil; public class UserSelectServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html"); Connection conn = DBUtil.getConn(); PreparedStatement ps = null; ResultSet rs = null; String sql = "select * from register"; List<People> peoples = new ArrayList<People>(); try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ People people = new People(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)); peoples.add(people); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.closeConn(conn, ps, rs); } HttpSession session = request.getSession(); session.setAttribute("peopleList", peoples); response.sendRedirect("userSelect.jsp"); } }

最后是jsp页面的展示((userSelect.jsp代码))

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="com.hnpi.bean.People"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'regist.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table> <thead> <tr><th>序号</th><th>姓名</th><th>密码</th><th>真实姓名</th><th>部门</th><th>角色</th><th colspan="2">操作</th></tr> </thead> <tbody> <% List<People> peoples = (List)session.getAttribute("peopleList"); for(People people :peoples){ %> <tr><td><%=people.getId() %></td><td><%=people.getName() %></td><td><%=people.getPwd() %></td> <td><%=people.getReallyName() %></td><td><%=people.getBranch() %></td><td><%=people.getRole() %></td><td><a href="#">删除</a></td><td><a href="#">更新</a></td></tr> <% } %> </tbody> </table> </body> </html>

上述代码和方法实现了foreach循环查询数据库里的信息并逐条显示。

了解更多关注我呦!!!

最新回复(0)