JAVAWEB+JSTL用户列表编辑功能

x33g5p2x  于2021-12-10 转载在 Java  
字(6.2k)|赞(0)|评价(0)|浏览(319)

首先说一下数据库更新操作的语句讲解:

我们想要修改数据库中的值需要写sql语句,然后为其中复制的一些是上面我们通过String定义好的,那么怎样写这样的sql语句呢?

讲解:

update users set username="+username+",password="+password+",email="+email+",grade="+grade+" where userId="+userId中的字段。

比如username如果是字符串的话,要加单引号update users set username=’"+username+"’…

所以我这里的写法应该是:

String sql ="update user set username ='"+username+"',password='"+passwd+"',sex = '"+sex+"',age = '"+age+"'where id = 1";

理解了上面的语句开始写编辑用户的界面:

前端用户编辑界面代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title></title>
    <style> .addVip { /*border: 1px solid black;*/ width: 100%; height: 1000px; background-color: #F0F3F4; padding-top: 50px; padding-left: 50px; } .rform { /*border: 1px solid black;*/ width: 800px; height: 600px; margin-top: 20px; margin-left: 150px; border-color: #E2E8EA; background-color: #FFFFFF; } .top1 { /*border: 1px solid black;*/ width: 800px; height: 50px; margin-top: -20px; background-color: #F6F8F8; } .top1 p { padding-top: 10px; font-family: "隶书"; font-size: 20px; padding-left: 5px; } .rif { /*border: 1px solid black;*/ width: 400px; height: 420px; margin-left: 250px; margin-top: 70px; color: #979797; } textarea { margin-left: 80px; margin-top: -30px; } .password input { width: 250px; height: 20px; } .name input { width: 250px; height: 20px; } .birthday input { width: 250px; height: 20px; } .home input { width: 250px; height: 20px; } .phone input { width: 250px; height: 20px; } .data1 p { padding-left: 40px; margin-top: -6px; } .home1 p { padding-left: 45px; margin-top: -2px; } .tijiao { margin-left: 60px; } .tijiao button { /*border: 1px solid black;*/ width: 70px; height: 40px; border-color: #23B7E5; background-color: #23B7E5; color: white; } </style>
</head>
<body>
<div class="addVip">
    <div class="rform">
        <div class="top1">
            <p>信息修改</p>
        </div>
        <div class="rif">
            <form action="edit" name="" method="post">
                <div class="name">
                    <p>ID:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="id" type="text"/>
                    </p>
                </div>
                <div class="name">
                    <p>
                        用户名:<input name="username" type="text"/>
                    </p>
                </div>

                <div class="birthday">
                    <p>
                    <div class="data1">
                    </div>
                    </p>
                </div>
                <div class="home">
                    <p>
                        密码:&nbsp;&nbsp;&nbsp;<input name="password" type="text"/>
                    </p>
                </div>
                <div class="sex">
                    <p>
                        性别:
                        <input type="radio" name="sex" value="男"/>男
                        <input type="radio" name="sex" value="女"/>女
                    </p>
                </div>
                <div class="home">
                    <p>
                        年龄:&nbsp;&nbsp;&nbsp;<input name="age" type="text"/>
                    </p>
                </div>
                <div class="tijiao">
                        <button type="submit">修改</button>
                </div>
            </form>

        </div>
    </div>
</div>
</body>
</html>

后端功能实现:

还是固定的步骤,连接好数据库然后sql语句进行操作,只不过这里需要edit.jsp界面的form表单action设置成edit然后后端@WebServlet("/edit")这样写:

package com.ftz.Demo.web;

import com.ftz.Demo.util.DBUtil;
import lombok.SneakyThrows;

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.Statement;

/** * @author ${范涛之} * @Description * @create 2021-12-09 1:26 */
@WebServlet("/edit")
public class EditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //设置服务器端以UTF-8编码进行输出
        resp.setCharacterEncoding("UTF-8");
        //设置浏览器以UTF-8编码进行接收,解决中文乱码问题
        resp.setContentType("text/html;charset=UTF-8");
        Integer id = Integer.valueOf(req.getParameter("id"));
        String username = req.getParameter("username");
        String passwd = req.getParameter("password");
        String sex = req.getParameter("sex");
        Integer age = Integer.valueOf(req.getParameter("age"));
        System.out.println(username+passwd+sex+age);
        Connection connection  = DBUtil.getConnection();
        String sql ="update user set username ='"+username+"',password='"+passwd+"',sex = '"+sex+"',age = '"+age+"'where id = '"+id+"'";
        Statement statement = connection.createStatement();
        int temp = statement.executeUpdate(sql);
        resp.sendRedirect("/user");
// req.getRequestDispatcher("user").forward(req,resp);
    }
}

编辑功能写好之后就是最难的用户界面回显功能:这里我们前端会用到JSTL传值。然后我们首先在后端写一个查询用户的方法,在DAO层:

/** * 查询全部数据 * * @throws SQLException */
    public List<User> findall() throws SQLException {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        connection = DBUtil.getConnection();
        String sql = "select * from user ";
        preparedStatement = connection.prepareStatement(sql);
        resultSet = preparedStatement.executeQuery();
        List<User> list = new ArrayList();
        while (resultSet.next()) {
            User user = new User();
            user.setId(Integer.valueOf(resultSet.getString(1)));
            user.setUsername(resultSet.getString(2));
            user.setPassword(resultSet.getString(3));
            user.setSex(resultSet.getString(4));
            user.setAge(Integer.valueOf(resultSet.getString(5)));
            list.add(user);
        }

        System.out.println(list.size());
        return list;
    }

在UserServlet层写:

public void userEdit(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        UserDao userDao = new UserDao();
        List<User> findall = userDao.findall();
        for (User user : findall) {
            System.out.println(user);
        }
        request.setAttribute("all", findall);
    }

在最前面调用一下:

// 用户编辑
            userEdit(request, response);
            request.getRequestDispatcher("user.jsp").forward(request,response);

前端JSTL:

运行截图:

相关文章

最新文章

更多