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: <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>
密码: <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>
年龄: <input name="age" type="text"/>
</p>
</div>
<div class="tijiao">
<button type="submit">修改</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
@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);
}
}
/** * 查询全部数据 * * @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;
}
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);
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121805010
内容来源于网络,如有侵权,请联系作者删除!
@WebServlet("/edit")
这样写: