azure011328

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

今天完成了一个webapp的实验以及项目

复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>新增学生</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<h1>新增学生</h1>
<form action="addsave.jsp" method="post">
    <label>学号:</label><input type="text" name="id" required><br>
    <label>姓名:</label><input type="text" name="name" required><br>
    <label>性别:</label><input type="text" name="gender" required><br>
    <label>生日:</label><input type="date" name="birthdate" required><br>
    <input type="submit" value="提交">
</form>
</body>
</html>
复制代码
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%request.setCharacterEncoding("utf-8");%>
<!DOCTYPE html>
<html>
<head>
    <title>添加结果</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<%
    int id = Integer.parseInt(request.getParameter("id"));
    String name = request.getParameter("name");
    String gender = request.getParameter("gender");
    String birthdate = request.getParameter("birthdate");

    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "web", "123456");
        String sql = "INSERT INTO students (id, name, gender, birthdate) VALUES (?, ?, ?, ?)";
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);
        pstmt.setString(2, name);
        pstmt.setString(3, gender);
        pstmt.setDate(4, Date.valueOf(birthdate));
        int result = pstmt.executeUpdate();
        if (result > 0) {
            out.println("添加成功!");
        } else {
            out.println("添加失败!");
        }
    } catch (Exception e) {
        response.sendRedirect("error.jsp?msg=" + e.getMessage());
    } finally {
        try {
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
%>
<a href="index.jsp">返回首页</a>
</body>
</html>
复制代码
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%request.setCharacterEncoding("utf-8");%>
<!DOCTYPE html>
<html>
<head>
    <title>删除结果</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<%
    int id = Integer.parseInt(request.getParameter("id"));

    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "web", "123456");
        String sql = "DELETE FROM students WHERE id=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);
        int result = pstmt.executeUpdate();
        if (result > 0) {
            out.println("删除成功!");
        } else {
            out.println("删除失败!");
        }
    } catch (Exception e) {
        response.sendRedirect("error.jsp?msg=" + e.getMessage());
    } finally {
        try {
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
%>
<a href="index.jsp">返回首页</a>
</body>
</html>
复制代码
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <title>编辑学生</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<h1>编辑学生</h1>
<%
    int id = Integer.parseInt(request.getParameter("id"));
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "web", "123456");
        String sql = "SELECT * FROM students WHERE id=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            String name = rs.getString("name");
            String gender = rs.getString("gender");
            Date birthdate = rs.getDate("birthdate");
%>
<form action="editsave.jsp" method="post">
    <input type="hidden" name="id" value="<%= id %>">
    <label>姓名:</label><input type="text" name="name" value="<%= name %>" required><br>
    <label>性别:</label><input type="text" name="gender" value="<%= gender %>" required><br>
    <label>生日:</label><input type="date" name="birthdate" value="<%= birthdate %>" required><br>
    <input type="submit" value="提交">
</form>
<%
        } else {
            out.println("未找到学生信息");
        }
    } catch (Exception e) {
        response.sendRedirect("error.jsp?msg=" + e.getMessage());
    } finally {
        try {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
%>
</body>
</html>
复制代码
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%request.setCharacterEncoding("utf-8");%>
<!DOCTYPE html>
<html>
<head>
    <title>修改结果</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<%
    int id = Integer.parseInt(request.getParameter("id"));
    String name = request.getParameter("name");
    String gender = request.getParameter("gender");
    String birthdate = request.getParameter("birthdate");

    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "web", "123456");
        String sql = "UPDATE students SET name=?, gender=?, birthdate=? WHERE id=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setString(2, gender);
        pstmt.setDate(3, Date.valueOf(birthdate));
        pstmt.setInt(4, id);
        int result = pstmt.executeUpdate();
        if (result > 0) {
            out.println("修改成功!");
        } else {
            out.println("修改失败!");
        }
    } catch (Exception e) {
        response.sendRedirect("error.jsp?msg=" + e.getMessage());
    } finally {
        try {
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
%>
<a href="index.jsp">返回首页</a>
</body>
</html>
复制代码
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>错误页面</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<h1>发生错误</h1>
<p><%= request.getParameter("msg") %></p>
<a href="index.jsp">返回首页</a>
</body>
</html>
复制代码
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%request.setCharacterEncoding("utf-8");%>
<!DOCTYPE html>
<html>
<head>
    <title>学生管理系统</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<h1>学生列表</h1>
<a href="add.jsp">新增学生</a>
<table border="1">
    <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>生日</th>
        <th>操作</th>
    </tr>
    <%
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "web", "123456");
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM students");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                Date birthdate = rs.getDate("birthdate");
    %>
    <tr>
        <td><%= id %></td>
        <td><%= name %></td>
        <td><%= gender %></td>
        <td><%= birthdate %></td>
        <td>
            <a href="edit.jsp?id=<%= id %>">编辑</a>
            <a href="del.jsp?id=<%= id %>">删除</a>
        </td>
    </tr>
    <%
            }
        } catch (Exception e) {
            response.sendRedirect("error.jsp?msg=" + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    %>
</table>
</body>
</html>
复制代码

 

posted on   淮竹i  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
 
点击右上角即可分享
微信分享提示