jsp数据库操作之数据更新

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/JY_WD/article/details/101125762

代码

还是承接那个select.jsp

update.jsp

<%--
  Created by IntelliJ IDEA.
  User: 长风
  Date: 2019/9/21
  Time: 20:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
    <title>数据更新</title>
</head>
<body>
<%!
    public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
    public static final String DBURL = "jdbc:mysql://localhost:3306/webstore?&useSSL=false&serverTimezone=UTC";
    public static final String DBUSER = "root";
    public static final String DBPASS = "123456";
%>
<%
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    String id = null;
%>
<%
    try {
        Class.forName(DBDRIVER);
        conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
        request.setCharacterEncoding("utf-8");
        id = request.getParameter("id");
        String sql_update = "select * from user_table where id='" + id + "'";
        //获取你要更新数据的id的数据库信息
        pst = conn.prepareStatement(sql_update);
        rs = pst.executeQuery();
        if (rs.next()) {
%>
<form action="doupdate.jsp?id=<%=rs.getString("id")%>" method="post">
    <%--切换到doupdate,显示要更新的数据信息--%>
    用户名:<input type="text" value="<%=rs.getString("用户名")%>" name="user">
    密码: <input type="text" value="<%=rs.getString("密码") %>" name="psw">
    用户类型:<select name="ty">
    <option value="管理员">管理员</option>
    <option value="普通用户">普通用户</option>
</select>
    <input type="submit" value="修改">
    <input type="reset" value="取消">
</form>
<%
        }
    } catch (Exception e) {
        out.println(e);
    }
%>

</body>
</html>

doupdate.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 长风
  Date: 2019/9/21
  Time: 20:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
    <title>数据更新</title>
</head>
<body>
<%!
    public static final String DBDRIVER="com.mysql.cj.jdbc.Driver";
    public static final String DBURL="jdbc:mysql://localhost:3306/webstore?&useSSL=false&serverTimezone=UTC";
    public static final String DBUSER="root";
    public static final String DBPASS="123456";
%>
<%
    Connection conn=null;
    PreparedStatement pst=null;
    int rs=0;
    String ids=null;
    String user=null;
    String psw=null;
    String ty=null;
%>
<%
    try{

        Class.forName(DBDRIVER);
        conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
        request.setCharacterEncoding("utf-8");
        ids=request.getParameter("id");
        user=request.getParameter("user");
        psw=request.getParameter("psw");
        ty=request.getParameter("ty");
        String sql_update="update user_table set 用户名='"+user+"',密码='"+psw+"',用户类型='"+ty+"'where id='"+ids+"'";
        pst=conn.prepareStatement(sql_update);
        rs=pst.executeUpdate();
        if(rs!=0){
            out.println("更新成功");
%>
<jsp:forward page="select.jsp"></jsp:forward>
<%--修改之后转到展示页面--%>
<%
        }
    }
    catch(Exception e){
        out.println(e);
    }

%>
</body>
</html>


运行结果

更新前
在这里插入图片描述
更新中:
在这里插入图片描述
更新后:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/JY_WD/article/details/101125762