文章目录
数据准备
前端页面原型
添加依赖库
封装 JDBC 工具类
package util;
import java.sql.*;
import java.util.ResourceBundle;
public class DBUtil {
// 读取配置文件
private static ResourceBundle bundle = ResourceBundle.getBundle("resources/db");
private static String driver = bundle.getString("driver");
private static String url = bundle.getString("url");
private static String user = bundle.getString("user");
private static String password = bundle.getString("password");
// 注册驱动
// 利用 类加载 时刻进行
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
// 设置一个方法,用于 返回 连接对象 , Connection
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
// 关流操作
public static void close(Connection connection, Statement statement, ResultSet resultSet){
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
查询学生
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="/stuid/list">系统启动</a>
<!-- 项目名 url -->
</body>
</html>
package com.yanyu;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/list")
public class ListStudent extends HttpServlet {
// ctrl o
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 固定写法
request.setCharacterEncoding("utf8");
// response.setCharacterEncoding("utf8");
// response.setContentType("text/html");
response.setContentType("text/html;charset=utf8");
PrintWriter out = response.getWriter();
// 连接数据库,查询学生
// 放大 数据相关变量的作用域
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
// 连接对象
connection = DBUtil.getConnection();
// 关闭自动提交事务
connection.setAutoCommit(false);
String sql = "select * from student";
// 操作对象
preparedStatement = connection.prepareStatement(sql);
// 操作语句,并处理结果集
resultSet = preparedStatement.executeQuery();
// 遍历结果及
// alt + 鼠标 下拖
out.println(" <!DOCTYPE html> ");
out.println("<html lang='en'>");
out.println("<head>");
out.println(" <meta charset='UTF-8'>");
out.println(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.println(" <title>Document</title>");
out.println(" <!-- 内嵌式 引入 CSS -->");
out.println(" <style>");
out.println(" h1{");
out.println(" text-align: center;");
out.println(" }");
out.println(" /* table 是具有块级属性,所以居住 margin */");
out.println(" table{");
out.println(" margin: 0 auto;");
out.println(" }");
out.println(" td{");
out.println(" text-align: center;");
out.println(" /* 居住 td 中的 文字 */");
out.println(" }");
out.println(" </style>");
out.println("</head>");
out.println("<body>");
out.println(" <h1>学生信息</h1>");
out.println(" <table border='1px' width='500px'>");
out.println(" <tr>");
out.println(" <td width='120px'>姓名</td>");
out.println(" <td width='120px'>学号</td>");
out.println(" <td width='180px'>专业</td>");
out.println(" <td width='200px'>操作</td>");
out.println(" </tr>");
while (resultSet.next()){
String name = resultSet.getString("name");
String stuid = resultSet.getString("stuid");
String major = resultSet.getString("major");
out.println(" <tr>");
out.println(" <td>"+ name +"</td>");
out.println(" <td>"+ stuid +"</td>");
out.println(" <td>"+ major +"</td>");
out.println(" <td width='200px'>");
out.println(" <a href='./add.html'>新增</a>");
out.println(" <a href='./modify.html'>修改</a>");
out.println(" <a href='javascript:void(0)' onclick='window.confirm('确认删除吗?')' >删除</a>");
out.println(" </td>");
out.println(" </tr>");
}
out.println(" </table>");
out.println(" ");
out.println("");
out.println("</body>");
out.println("</html>");
connection.commit();
} catch (SQLException e) {
// 回滚事务
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
DBUtil.close(connection,preparedStatement,resultSet);
}
}
}