首页 > 其他分享 >2023.3.2每日总结

2023.3.2每日总结

时间:2023-03-02 21:48:21浏览次数:41  
标签:总结 每日 request public 2023.3 sql import servlet id

  1. User实体类:
    简单的定义属性,然后生成getter/setter方法
public class User {
    private int id;
    private String name;
    private int age;

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    private String pwd;

    public User() {

    }

    public User(int id, String name, int age, String pwd) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.pwd = pwd;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

  1. 登录界面 index.jsp
    简单的登录界面 登录由ajax发起
<%--
  Created by IntelliJ IDEA.
  User: Rtwo
  Date: 2020/3/23
  Time: 20:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
        $(function () {
            $("#login").click(function () {
                var username = $("#username").val();//取值
                var password = $("#password").val();
                if (!username) {
                    alert("用户名必填!");
                    $("#username").focus();//获取焦点
                    return;
                }
                if (!password) {
                    alert("密码必填!");
                    $("#password").focus();//获取焦点
                    return;
                }
                $.ajax({
                    url: "login",
                    type: "post",
                    data: {
                        username: username,
                        password: password
                    },
                    dataType: "text",
                    success: function (data) {
                        if (data === '1') {
                            window.location.href = "userlist?name="+username+"&pwd="+password;
                        } else {
                            alert("用户名或密码错误!请重新输入!");
                        }
                    },
                    error: function () {
                        alert("错误");
                    }
                });
            });
        });
    </script>
    <title>登录</title>
</head>
<body>
<center>
    <h1>登录</h1>
    <table border="0">
        <tr>
            <td>账号:</td>
            <td><input type="text" class="loginform_input" id="username"></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="password" class="loginform_input" id="password">
            </td>
        </tr>
    </table>
    <br>
    <input id="login" type="submit" class="loginform_submit" value="登录">
</center>
</body>
</html>

 

  1. UserListServlet.class
    登录成功后转到这里,从数据库查询用户表,再将查到的数据传给main.jsp
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;

import com.bosssoft.hr.train.web.entity.User;
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;


/**
 * @author Rtwo
 */
@Slf4j
@WebServlet("/UserListServlet")
public class UserListServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        Connection conn = DbUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from t_user";
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        List<User> users = new ArrayList<>();
//        int index = -1;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4));
                users.add(user);
            }
//            for (int i = 0; i < users.size(); i++) {
//                if (name.equals(users.get(i).getName()) && pwd.equals(users.get(i).getPwd())) {
//                    index = i;
//                }
//            }
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            DbUtil.closeConn(conn, ps, rs);
        }
        HttpSession session = request.getSession();
        session.setAttribute("userList", users);
//        session.setAttribute("current", index);
        response.sendRedirect("main.jsp");
    }
}

 

  1. 主界面 main.jsp
    在此界面可以操作增删查改
<%@ page import="com.bosssoft.hr.train.web.entity.User" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: 13701
  Date: 2020/3/23
  Time: 19:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>操作用户</title>

</head>
<body>

<%
    //    int index = (int) session.getAttribute("current");
    List<User> users = (List) session.getAttribute("userList");
%>
<%--<center>--%>
<%--    <a>当前用户:</a>--%>
<%--    <a id="current"><%=users.get(index).getName()%>--%>
<%--    </a>--%>
<%--</center>--%>
<table border="1" cellspacing="0" align="center" width="1000dp"
       height="500dp">
    <thead>
    <tr>
        <td>
            编号
        </td>
        <td>
            姓名
        </td>
        <td>
            年龄
        </td>
        <td>
            操作
        </td>
    </tr>
    </thead>
    <tbody>
    <tr>

        <%
            for (User user : users) {
        %>
        <td><%=user.getId()%>
        </td>
        <td><%=user.getName()%>
        </td>
        <td><%=user.getAge()%>
        </td>
        <td>
            <a href="delete?id=<%=user.getId() %>">删除</a>
            <a href="updateshow?id=<%=user.getId() %>">更新</a>
        </td>
    </tr>
    <%
        }
    %>
    </tbody>
</table>
<center>
    <a href="insert.jsp">新增</a>
    <a href="search.jsp">查询</a>
</center>
</body>
</html>

 

  1. 删除用户 DeleteServlet.class
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;

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 java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Rtwo
 */
@Slf4j
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //接受到将要删除的id
        String id = request.getParameter("id");

        Connection conn = DbUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "delete from t_user where id = ?";

        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            response.sendRedirect("UserListServlet");
        } catch (SQLException e) {
            log.info(e.getMessage());
        } finally {
            DbUtil.closeConn(conn, ps, rs);
        }
    }
}

 

  1. 更新用户界面逻辑 UpdateShowServlet.class
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;

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 java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Rtwo
 */
@Slf4j
@WebServlet("/UpdateShowServlet")
public class UpdateShowServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }


    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        String id = request.getParameter("id");
        //获取要删除数据的id
        Connection conn = DbUtil.getConn();
        //查询出该id的所有字段内容
        String sql = "select * from t_user where id=?";
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                request.setAttribute("id", rs.getInt(1));
                request.setAttribute("name", rs.getString(2));
                request.setAttribute("age", rs.getInt(3));
                /*
                 *将数据发送到一个jsp页面,在该页面进行人工的数据的修改
                 */
                request.getRequestDispatcher("update.jsp").forward(request, response);
            }
        } catch (SQLException e) {
           log.info(e.getMessage());
        } finally {
            DbUtil.closeConn(conn, ps, rs);
        }

    }
}

 

  1. 更新用户界面 update.jsp
<%@ page import="com.bosssoft.hr.train.web.entity.User" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: 13701
  Date: 2020/3/23
  Time: 19:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>更新用户</title>

</head>
<style type="text/css">
    .input1 {
        border: 1px solid #CDC28D; /* 输入框表框颜色*/
        background-color: #F2F9FD; /* 输入框背景颜色*/
        height: 40px; /*输入框高度*/
        width: 200px; /*输入框宽度*/
        padding-top: 4px;
        /*输入框里的文字和输入框上边之间的距离
             font-family: Arial, Helvetica, sans-serif; /*输入框文字类型*/
        font-size: 12px; /*输入框内文字大小*/
        padding-left: 10px; /*输入框里的文字和输入框左边之间的距离*/
    }

    .input2 {
        border: 1px solid #1E1E1E; /* 输入框表框颜色*/
        background-color: #F2F9FD; /* 输入框背景颜色*/
        height: 30px; /*输入框高度*/
        width: 80px; /*输入框宽度*/
    }
</style>

<body>
<form action="UpdateServlet" method="post">
    <table align="center">
        <tr height="150dp">
            <td></td>
        </tr>
        <tr>
            <td colspan="2"><h1>用户更新</h1></td>
        </tr>
        <tr>
            <td>ID:</td>
            <td><input class="input1" type="text" name="id" readonly="readonly"
                       value="<%=request.getAttribute("id") %>"/></td>
        </tr>
        <tr>
            <td>姓名:</td>
            <td><input class="input1" type="text" name="name" value="<%=request.getAttribute("name") %>"/></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input class="input1" type="text" name="age" value="<%=request.getAttribute("age") %>"/></td>
        </tr>
        <tr align="center">
            <td colspan="2"><input class="input2" type="submit" value="更新"/></td>
        </tr>
    </table>
</form>
</body>
</html>

 

  1. 更新用户信息 UpdateServlet.class
    将更新后的数据写到数据库,然后返回主界面
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;

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 java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Rtwo
 */
@Slf4j
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }


    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        //获取到要更新的数据

        Connection conn = DbUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        //更新语句
        String sql = "update t_user set name=?, age=? where id=?";

        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setInt(2, Integer.parseInt(age));
            ps.setInt(3, Integer.parseInt(id));
            response.sendRedirect("UserListServlet");

        } catch (SQLException e) {
            log.info(e.getMessage());
        } finally {
            DbUtil.closeConn(conn, ps, rs);
        }
    }
}
 

标签:总结,每日,request,public,2023.3,sql,import,servlet,id
From: https://www.cnblogs.com/fuchuchu/p/17173637.html

相关文章

  • Android学习-每日打卡APP-初步
    今天开始着手做个人作业-每日打卡APP今天首先实现了主要页面和简单功能,项目的结构基本确定,进展刚开始初步实现主要页面和页面跳转,之后的进展见之后的博客吧 ......
  • 每日总结9
    今天学习时长没有昨天长,上午工程数学,学习了如何求函数的梯度和海森矩阵的求法,和一些其他的矩阵,并且了解了一种简单的算法,下午体育课高强度运动,进行了激烈的足球对抗,增强了......
  • 2023年3月2日学习总结
    今天学习了使用监听器实现点击不同按钮的功能  学习了如何创建数据库和表,如何添加数据。 ......
  • 3.2每日总结——安卓
       今天在做第一阶段的安卓作业。   尝试用安卓连接数据库。这个我感觉和JAVA连接数据库大同小异。   今天只写了一个.java文件,还没有完全搞明白这个......
  • 2022.3.2周四每日博客
    今天实现了单表的jdbc连接数据库增删改查,并开始向多表实现增删改查逐步靠拢由于·身体抱恙,一直处于发烧状态,下午请假没有去上体育课,在宿舍睡了一身汗今天没有花大量时间......
  • 每日总结3.2
    每日总结:所花时间:2h代码量:0行博客量:1篇————————————~~~~~~刷~~~~~————————————————今天除了课上的工程数学之外,我简......
  • 3.2号今日总结
    1.选项菜单(选项菜单)1)如何使用选项菜单?答:非常简单,重写两个方法就好,其实这两个方法我们在创建项目的时候就会自动生成~他们分别是:publicboolean onCreateOptionsMen......
  • 工具总结
    工具总结求学十余载,生活中的一切皆与吾言工具之重。科技的原动力为懒惰,科技的结晶为工具。从幼儿时期的碗勺乃至于成年时期的电脑,工具伴随我们一生;念于2022年9月13日清晨......
  • 每日一道思维题——1725H - Hot Black Hot White
    题意:给定n个整数Ai,定义一种运算concat(Ai,Aj)讲AiAj拼接在一起如concat(12,34)=1234若i,j上颜色不同有运算concat(Ai,Aj)×concat(Aj,Ai)+Ai×Aj≡Zmod3思路:  代码:......
  • 每日总结-23.3.2
    今天学习了一点关于操作sqllite数据库相关操作,以及相应类的写法importandroid.content.Context;importandroid.database.sqlite.SQLiteDatabase;importandroid.databas......