本周总结

看了web的实验3要求,我才发现原来只用jsp也能实现简单的增删改查,并不是一定要有后端。

示例:

1.index.jsp

<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
<%@ page errorPage="error.jsp"%>
<html>
<head>
  <title>学生管理系统</title>
  <style>
    body{
      background-image:url("image/6.jpg");
      background-size:cover;
    }
    h1{
      font-size:50px;
      color:darksalmon;
    }
    a{
      color:lightyellow;
      text-decoration: none;
      font-size:20px;
    }
  </style>
  <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div align="center">
<h1>学生管理系统</h1>
<a href="addstudent.jsp">添加学生信息</a>
<br />
<br />
<table style="width: 30%;">
  <tr>
    <th>学号</th>
    <th>姓名</th>
    <th>性别</th>
    <th>生日</th>
  </tr>
  <%
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web3?useUnicode=true&characterEncoding=utf-8", "root", "lyf123456");
    //使用Statement对象
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from info");

            /*
            PreparedStatement stmt = con.prepareStatement("select * from bookinfo");
            ResultSet rs = stmt.executeQuery();
            */
    while (rs.next()) {
      String id = rs.getString(1);
      out.println("<tr><td>" + rs.getString(1)+"</td><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>"
              + rs.getString(4) + "</td><td><a href='updatestudent.jsp?id=" + id
              + "'>修改</a>&nbsp;<a href='deletestudent.jsp?id=" + id + "'>删除</a></td></tr>");
    }
    rs.close();
    stmt.close();
    con.close();
  %>

</table>
<br />
<hr />
</div>
</body>
</html>

2.addstudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <style>
        body{
            background-image:url("image/6.jpg");
            background-size:cover;
        }
        h1{
            font-size:45px;
            color:coral;
        }
        a{
            color:orchid;
            text-decoration: none;
            font-size:20px;
        }
        input{
            background-color: antiquewhite;
            border-radius: 10px;
            border-color: antiquewhite;
        }
        button{
            background-color: lightskyblue;
            border-radius: 10px;
            border-color: lightskyblue;
        }

    </style>
</head>
<body>
<%
    Object message = request.getAttribute("message");
    if (message != null && !"".equals(message)) {
%>
<script type="text/javascript">
    alert("<%=request.getAttribute("message")%>"); //弹出对话框
</script>
<%
    }
%>
<div align="center">
    <br><br><br><br><br><br><br>
    <h1>&nbsp;添加学生信息</h1>
    <a href="index.jsp">&nbsp;&nbsp;返回主页</a>
    <form action="addsave.jsp" method="post">
        <div>
            学号<input type="text" id="id" name="id" />
        </div><br>
        <div>
            姓名<input type="text" id="name" name="name" />
        </div><br>
        <div>
            性别<input type="radio"  name="sex" value=""/><input type="radio"  name="sex" value="" /></div><br>
        <div>
            生日<input type="text" id="birth" name="birth" />
        </div><br>
        <div>
            &nbsp;&nbsp;&nbsp;<button type="submit">添加</button>
        </div>
    </form>
</div>
</body>
</html>

3.addsave.jsp

<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%>
<html>
<head>
    <title>添加学生信息</title>
    <style>
        body{
            background-image:url("image/6.jpg");
            background-size:cover;
        }
        h1{
            font-size:45px;
            color:coral;
            text-align: center;
        }
        a{
            color:orchid;
            text-decoration: none;
            font-size:20px;
            text-align: center;
        }
    </style>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String sex = request.getParameter("sex");
    String birth = request.getParameter("birth");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web3?useUnicode=true&characterEncoding=utf-8", "root", "lyf123456");
    //使用Statement对象
//         Statement stmt = con.createStatement();
//         String sql = "insert into bookinfo(bookname,author,price) values('" + bookname + "','" + author + "'," + price + ")";
//         System.out.println(sql);
//         int i = stmt.executeUpdate(sql);


    PreparedStatement stmt = con.prepareStatement("insert into info(id,name,sex,birth) values(?, ?, ?, ?)");

    stmt.setString(1, id);
    stmt.setString(2, name);
    stmt.setString(3, sex);
    stmt.setString(4, birth);
    int i = stmt.executeUpdate();
    if (i == 1) {
        out.println("<h1>添加成功!</h1><br/>");
        out.println("<a href='index.jsp'>返回首页</a>");
    } else {
        out.println("<h1>添加失败!</h1><br/>");
        out.println("<a  href='addstudent.jsp'>重新添加</a>");
    }
    stmt.close();
    con.close();
%>
</body>
</html>

 

posted @ 2022-05-06 15:01  Lindseyyip  阅读(20)  评论(0)    收藏  举报