jsp數據庫操作之數據更新


代碼

還是承接那個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>


運行結果

更新前
在這里插入圖片描述
更新中:
在這里插入圖片描述
更新后:
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM