2023/5/26总结

今天主要是上课,上午上了计算机网络的实验和概率论最后的章节,下午进行了web实验。实验四代码:

1) index. jsp文件代码

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

    <%@page import="java.util.List" %>

    <%@ page language="java" import="Javabean.Lab04" %>

<%@ page language="java" import="Dao.Dao" %>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>学生信息列表</title>

<style>

    body 

    {

     background-image:url('1.jpg');

     background-color:#cccccc;

     background-repeat:no-repeat;

     background-size:cover;

    }

    table.serif{font-family:"宋体";}

    div.serif{font-family:"宋体";}

    #tp{

     width:40%;

    }

    #tp

    {

     height:100px;

     vertical-align:center;

    }

</style>

</head>

<body>

<div align="center">

        <h1 style="color: black;">学生信息列表</h1>

<table align="center" class="serif">

<td>

<a href="add.html">添加学生信息</a>

</td>

</table>

</div>

<% 

Dao dao=new Dao();

List<Lab04> Lab03 = dao.liulan();  //此处是取出所存储的数据

request.getSession().setAttribute("Lab03",Lab03);

    %>

<div align="center" class="serif">

        <table class="tb" id="tp">

            <tr>

                <td>学号</td>

                <td>姓名</td>

                <td>性别</td>

                <td>生日</td>

                <td>管理</td>

            </tr>

            <c:forEach items="${Lab03}" var="item">

                <tr>

                    <td>${item.id}</td>

                    <td>${item.name}</td>

                    <td>${item.sex}</td>

                    <td>${item.birth}</td>

                    <td>

                    <a href="edit.jsp?id=${item.id}&name=${item.name}&sex=${item.sex}&birth=${item.birth}">修改</a>

                    <a href="Lab04Servlet?method=del&id=${item.id}">删除</a>

                    </td>

                </tr>

            </c:forEach>

        </table>

    </div>

</body>

</html>

2) add. html文件代码

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>增加</title>

<style>

    body 

    {

     background-image:url('1.jpg');

     background-color:#cccccc;

     background-repeat:no-repeat;

     background-size:cover;

    }

    table.serif{font-family:"宋体";}

    input.serif{font-size:12px;padding:3px;background: #f8f9ff;border: 1px solid #c4c9fd;color:#6C63A3;}

    select.serif{font-size:12px;padding:3px;background: #f8f9ff;border: 1px solid #c4c9fd;color:#6C63A3;}

</style>

</head>

<body>

  <table align="center" border="0px" cellpadding="10px" cellspacing="10px" class="serif">

  <form action="Lab04Servlet?method=add"  method="post"  onsubmit="return check()">

   <tr>

    <td>学号:</td>

    <td><input type="text" name="id" id="id" class="serif"></td>

   </tr>

   <tr>

   <td>姓名:</td>

   <td><input type="text" name="name" id="name" class="serif"></td>

   </tr>

   <tr>

   <td>性别</td>

   <td>

   <select name="sex" class="serif">

   <option value="男">男</option>

   <option value="女">女</option>

    </select>

   </td>

   </tr>

   

   <tr>

   <td>出生日期:</td>

   <td><input type="date" name="birth" id="birth" class="serif"></td>

   </tr>

   <tr>

 

    <tr align="center">

    <th colspan="2">

    <input type="submit" value="提交">

    <input type="reset" value="重置">

    </th>

    </tr>

    </form>

</table>

</body>

</html>

3) edit. 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('1.jpg');

     background-color:#cccccc;

     background-repeat:no-repeat;

     background-size:cover;

    }

    table.serif{font-family:"宋体";}

    input.serif{font-size:12px;padding:3px;background: #f8f9ff;border: 1px solid #c4c9fd;color:#6C63A3;}

</style>

</head>

<body>

<%

String id=request.getParameter("id");

String name=request.getParameter("name");

String sex=request.getParameter("sex");

String birth=request.getParameter("birth");

%>

<table align="center" border="0px" cellpadding="10px" cellspacing="10px" class="serif">

  <form action="Lab04Servlet?method=edit"  method="post"  onsubmit="return check()">

   <tr>

   <td>学号:</td>

   <td><input type="text" name="id" id="id" value=<%= id%> class="serif"></td>

   </tr>

   

   <tr>

   <td>姓名:</td>

   <td><input type="text" name="name" id="name" value=<%= name%> class="serif"></td>

   </tr>

   

   <tr>

   <td>性别:</td>

   <td><input type="text" name="sex" id="sex" value=<%= sex%> class="serif"></td>

   </tr>

   

   <tr>

   <td>生日:</td>

   <td><input type="date" name="birth" id="birth" value=<%= birth%> class="serif"></td>

   </tr>

   <tr align="center">

    <th colspan="2">

    <input type="submit" value="提交">

    <input type="reset" value="重置">

    </th>

    </tr>

    </form>

</table>

</body>

</html>

4) Dao. java文件代码

package Dao;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

 

import DBUtil.DBUtil;

import Javabean.Lab04;

public class Dao {

public List<Lab04> liulan() {

        String sql = "select * from stu";

        List<Lab04> list = new ArrayList<>();

        Connection conn = DBUtil.getConn();

        Statement state = null;

        ResultSet rs = null;

 

        try {

            state = conn.createStatement();

            rs = state.executeQuery(sql);

            Lab04 bean = null;

            while (rs.next()) {

                String id = rs.getString("id");

                String name = rs.getString("name");

                String sex = rs.getString("sex");

                String birth = rs.getString("birth");

                bean = new Lab04(id,name,sex,birth);

                list.add(bean);

                }

            }catch (SQLException e) {

                e.printStackTrace();

            } finally {

                DBUtil.close(rs, state, conn);

            }

        

        return list;

    }

public boolean add(Lab04 Lab)

    {

          Connection conn = DBUtil.getConn();

          PreparedStatement pstmt = null;

          boolean f = false;

          int a=0;

          try {

              String sql = "insert into stu(id,name,sex,birth) value(?,?,?,?)";

              pstmt = conn.prepareStatement(sql);

              pstmt.setString(1, Lab.getId());

              pstmt.setString(2, Lab.getName());

              pstmt.setString(3, Lab.getSex());

              pstmt.setString(4, Lab.getBirth());

             a = pstmt.executeUpdate();

          }

          catch(SQLException e) {

              e.printStackTrace();

          }

          finally {

              DBUtil.close(pstmt, conn);

          }

          if(a>0)

          f=true;

          

          return f;

    }

public boolean xiugai(String id,String name,String sex,String birth) {

System.out.println(birth);

        String sql = "update stu set name='" + name + "',sex='"+sex+"',birth='"+birth+"'where id='" + id + "'";

    Connection conn = DBUtil.getConn();

    Statement state = null;

    boolean f = false;

    int a = 0;

    try {

        state = conn.createStatement();

        a = state.executeUpdate(sql);

        System.out.println(a);

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        DBUtil.close(state, conn);

    }

    

    if (a > 0) {

        f = true;

    }

    System.out.println(f);

    return f;

    }

public boolean shanchu(String id) {

        String sql = "delete from stu where id='" + id + "'";

    Connection conn = DBUtil.getConn();

    Statement state = null;

    boolean f = false;

    int a = 0;

    try {

        state = conn.createStatement();

        a = state.executeUpdate(sql);

        System.out.println(a);

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        DBUtil.close(state, conn);

    }

    

    if (a > 0) {

        f = true;

    }

    System.out.println(f);

    return f;

    }

}

5) DBUtil. java文件代码

package DBUtil;

 

import java.sql.*;

 

/**

 * 鏁版嵁搴撹繛鎺ュ伐鍏�

 * @author Hu

 *

 */

public class DBUtil {

    

    public static String url =  "jdbc:mysql://localhost:3306/world?serverTimezone=UTC";

    public static String user = "root";

    public static String password = "88888888";

    

    public static Connection getConn () {

        Connection conn = null;

        

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");//鍔犺浇椹卞姩

            conn = DriverManager.getConnection(url, user, password);

        } catch (Exception e) {

            e.printStackTrace();

        }

        

        return conn;

    }

    

    /**

     * 鍏抽棴杩炴帴

     * @param state

     * @param conn

     */

    public static void close (PreparedStatement preparedState, Connection conn) {

        if (preparedState != null) {

            try {

                preparedState.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    

    public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) {

        if (rs != null) {

            try {

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (preparedState != null) {

            try {

                preparedState.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

 

    /**

     * 鍏抽棴杩炴帴

     * @param state

     * @param conn

     */

    public static void close (Statement state, Connection conn) {

        if (state != null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    

    public static void close (ResultSet rs, Statement state, Connection conn) {

        if (rs != null) {

            try {

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (state != null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    

    public static void main(String[] args) throws SQLException {

        Connection conn = getConn();

        PreparedStatement preparedStatement = null;

        ResultSet rs = null;

        String sql ="select * from xuanke";

        preparedStatement = conn.prepareStatement(sql);

        rs = preparedStatement.executeQuery();

        if(rs.next()){

            System.out.println("鏁版嵁搴撲负绌�");

        }

        else{

            System.out.println("鏁版嵁搴撲笉涓虹┖");

        }

    }

}

6) Lab04Servlet.java文件代码

package Lab04Servlet;

import java.io.IOException;

import java.util.List;

 

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import Dao.Dao;

import Javabean.Lab04;

 

 

 

 

7) @WebServlet("/Lab04Servlet")
public class Lab04Servlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    Dao dao = new Dao();
    protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if("add".equals(method)) {
            add(req,resp);
        }else if("edit".equals(method)) {
            edit(req,resp);
        }else if("del".equals(method)) {
            del(req,resp);
        }
    }
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     req.setCharacterEncoding("utf-8");
     String id = req.getParameter("id");
     String name = req.getParameter("name");
     String sex = req.getParameter("sex");
     String birth = req.getParameter("birth");
     Lab04 Lab=new Lab04(id,name,sex,birth);
     Dao dao=new Dao();
        if(dao.add(Lab)) {
            req.setAttribute("Lab",Lab);
            req.setAttribute("message","添加成功" );
            req.getRequestDispatcher("success.html").forward(req, resp);
        }else {
            req.setAttribute("message","姓名重复,请重新输入" );
            req.getRequestDispatcher("failure.html").forward(req, resp);
        }
    }
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     req.setCharacterEncoding("utf-8");
     String id = req.getParameter("id");
     String name = req.getParameter("name");
     String sex = req.getParameter("sex");
     String birth = req.getParameter("birth");
        if(dao.xiugai(id,name,sex,birth)) {
            req.setAttribute("message","添加成功" );
            req.getRequestDispatcher("success.html").forward(req, resp);
        }else {
            req.setAttribute("message","姓名重复,请重新输入" );
            req.getRequestDispatcher("failure.html").forward(req, resp);
        }
    }
    private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     req.setCharacterEncoding("utf-8");
     String id = req.getParameter("id");
        if(dao.shanchu(id)) {
            req.setAttribute("message","添加成功" );
            req.getRequestDispatcher("success.html").forward(req, resp);
        }else {
            req.setAttribute("message","姓名重复,请重新输入" );
            req.getRequestDispatcher("failure.html").forward(req, resp);
        }
    }
}

posted @   liu_ru_jun  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示