日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

循环输出

發布時間:2025/3/12 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 循环输出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用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 blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(rs!=null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.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 blocke.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循環查詢數據庫里的信息并逐條顯示。

    了解更多關注我呦!!!

    總結

    以上是生活随笔為你收集整理的循环输出的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。