准備
通過Mysql建立數據庫webstore,建立表格user_table。如下圖

注意:上面的列名我用的漢字,可以轉換為英文(推薦)后面的代碼,要與這個列名一一對應,自己體會。
代碼:
select.jsp
<%--
Created by IntelliJ IDEA.
User: 長風
Date: 2019/9/21
Time: 19:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
<title>管理員頁面</title>
</head>
<body>
<%!
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
// 驅動路徑
public static final String DBURL = "jdbc:mysql://localhost:3306/webstore?&useSSL=false&serverTimezone=UTC";
// webstore是我數據庫的名字,?&useSSL=false&serverTimezone=UTC防止報錯和亂碼。
public static final String DBUSER = "root";
// 數據庫用戶名
public static final String DBPASS = "123456";
// 數據庫密碼
%>
<%
/*這里的一下參數也可以去下面定義*/
Connection conn = null;
/*定義一個創建連接的變量,初始值設為空,當需要連接時,通過 Connection conn=DriverManager.getConnection(URL,Username,Password); 去創建連接, 方便后面關閉操作*/
PreparedStatement pst = null;
/*Statement和PreparedStatament的區別 Statement執行查詢調用方法executeQuery(sql) 執行更刪改調用方法executeUpdate(sql) PreparedStatement執行查詢調用方法executeQuery() 執行更刪改調動方法executeUpdate()*/
ResultSet rs = null;
%>
<%
try {
Class.forName(DBDRIVER);
//注冊驅動
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
//獲取鏈接
request.setCharacterEncoding("utf-8");
String sql_select = "select * from user_table";
//獲取操作數據庫對象,注意這里的user_table是我數據庫里面的一個表
pst = conn.prepareStatement(sql_select);
rs = pst.executeQuery();
//執行sql_select,獲取結果集
%>
<table border="1">
<tr>
<td>id</td>
<td>用戶名</td>
<td>密碼</td>
<td>用戶類型</td>
<td colspan="2" align="center">數據操作</td>
</tr>
<%
while (rs.next()) {
//處理結果
%>
<tr>
<td><%= rs.getString(1) %>
</td>
<td><%= rs.getString("用戶名")%>
</td>
<td><%= rs.getString("密碼")%>
</td>
<td><%= rs.getString("用戶類型")%>
</td>
<td>
<button><a href="delete.jsp?id=<%=rs.getString("id")%>">刪除該記錄</a></button>
</td>
<td>
<button><a href="update.jsp?id=<%=rs.getString("id")%>">更新該記錄</a></button>
</td>
</tr>
<%
}
} catch (Exception e) {
out.println(e);
}
%>
<tr>
<td>
<button><a href="insert.jsp">插入</a></button>
</td>
</tr>
</table>
</body>
</html>
運行結果

