stuid学生信息
文章目录
- 前端准备
- MySQL数据库
- 封装JDBC 连接工具类 DBUtil
- 查寻学生
前端准备
结构
<!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="./list.html">系统启动</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 内嵌式 引入 CSS -->
<style>
h1{
text-align: center;
}
/* table 是具有块级属性,所以居住 margin */
table{
margin: 0 auto;
}
td{
text-align: center;
/* 居住 td 中的 文字 */
}
</style>
</head>
<body>
<h1>学生信息</h1>
<table border="1px" width="500px">
<tr>
<td width="120px">姓名</td>
<td width="120px">学号</td>
<td width="180px">专业</td>
<td width="200px">操作</td>
</tr>
<tr>
<td>小明</td>
<td>1001</td>
<td>计算机应用技术</td>
<td width="200px">
<a href="./add.html">新增</a>
<a href="./modify.html">详情</a>
<a href="javascript:void(0)" onclick="window.confirm('确认删除吗?')" >删除</a>
</td>
</tr>
</table>
</body>
</html>
<!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>
<h1>更新学生信息</h1>
<form action="">
<!-- 原来的信息是来自于数据库的 -->
姓名(原来): <input type="text">
<br>
学号(原来): <input type="text">
<br>
专业(原来): <input type="text">
<br>
<!-- 改完后的信息,需要交到 服务器 name -->
姓名:
<input type="text" name="username">
<br>
学号:
<input type="text" name="stuid" >
<br>
专业:
<input type="text" name="zhuanye">
<br><br>
<input type="submit" value="确认修改" >
</form>
</body>
</html>
<!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>
<h1>添加学生信息</h1>
<form action="">
<!-- 改完后的信息,需要交到 服务器 name -->
姓名:
<input type="text" name="username">
<br>
学号:
<input type="text" name="stuid" >
<br>
专业:
<input type="text" name="zhuanye">
<br><br>
<input type="submit" value="确认新增" >
</form>
</body>
</html>
MySQL数据库
封装JDBC 连接工具类 DBUtil
package resources;
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);
// 本质上: 在 进行类加载的时候,给我把 com.mysql.cj.jdbc.Driver
// com.mysql.cj.jdbc. 下面的 Driver这类 com.mysql.cj.jdbc.Driver . class 加载到
// 方法区内存中(元空间)
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 方法的设计者角度 设计连接对象的静态方法
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection(url, user, password);
// 此处的异常不处理,抛出去,谁调用我,谁来处理异常
return connection;
}
// 设计关闭流的静态方法 方法设计者
public static void close(ResultSet rs, Statement st,Connection con){
// Statement 是 PreparedStatement ps 的父接口
// 调用者 用 Statement PreparedStatement 都支持
// 若第二个参数为 PreparedStatement ps 那么调用者 用 Statement 时会报错,或者要强制类型转换
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
查寻学生
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 resources.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@WebServlet("/list")
public class List extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// 响应的类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
Statement st = null;
ResultSet rs = null;//查询结果集对象
try {
con = DBUtil.getConnection();
con.setAutoCommit(false);
String sql = "select * from student";
st = con.createStatement();
rs = st.executeQuery(sql);
out.print(" <!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.print(" <title>Document</title>");
out.print(" <!-- 内嵌式 引入 CSS -->");
out.print(" <style>");
out.print(" h1{");
out.print(" text-align: center;");
out.print(" }");
out.print(" /* table 是具有块级属性,所以居住 margin */");
out.print(" table{");
out.print(" margin: 0 auto;");
out.print(" }");
out.print(" td{");
out.print(" text-align: center;");
out.print(" /* 居住 td 中的 文字 */");
out.print(" }");
out.print(" </style>");
out.print("</head>");
out.print("<body>");
out.print(" <h1>学生信息</h1>");
out.print(" <table border='1px' width='500px'>");
out.print(" <tr>");
out.print(" <td width='120px'>学号</td>");
out.print(" <td width='180px'>专业</td>");
out.print(" <td width='200px'>操作</td>");
out.print(" </tr>");
while (rs.next()){
String name = rs.getString("name");
String stuid = rs.getString("stuid");
String zhuanye = rs.getString("zhuanye");
out.print(" <tr>");
out.print(" <td>"+name+"</td>");
out.print(" <td>"+stuid+"</td>");
out.print(" <td>"+zhuanye+"</td>");
out.print(" <td width='200px'>");
out.print(" <a href='./add.html'>新增</a>");
out.print(" <a href='./modify.html'>详情</a>");
out.print(" <a href='javascript:void(0)' οnclick='window.confirm('确认删除吗?')' >删除</a>");
out.print(" </td>");
out.print(" </tr>");
}
out.print(" </table>");
out.print("</body>");
out.print("</html>");
out.print(" ");
con.commit();
} catch (SQLException throwables) {
// 回滚事务
if (con != null) {
try {
con.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
throwables.printStackTrace();
}
}
}