• 12月11日学习日志


    今天学习了java web学生信息系统的修改操作。

    jsp代码如下:

    <%@page import="com.servlet.Student" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%
        Student s=(Student)request.getAttribute("student");
    %>
    <form action="editsuccess" method="post">
    <input type="hidden" name="id" value="<%=s.getId()%>"><br>
        姓名:<input type="text" name="name"><br>
        年龄:<input type="text" name="age"><br>
        学号:<input type="text" name="num"><br>
    <input type="submit" value="提交">
    </form>
    </body>
    </html>

    servlet代码如下:

    package com.servlet;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    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 com.jdbc.jdbcUtil;
    
    /**
     * Servlet implementation class edit
     */
    @WebServlet("/edit")
    public class edit extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public edit() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            Integer id=Integer.parseInt(request.getParameter("id").toString());
            try {
                Connection con=jdbcUtil.getcon();
                String sql="select * from stu where id=?";
                PreparedStatement pst=con.prepareStatement(sql);
                pst.setInt(1, id);
                ResultSet rs=pst.executeQuery();
                Student s=null;
                while(rs.next()) {
                    Integer newid=rs.getInt("id");
                    String name=rs.getString("name");
                    Integer age=rs.getInt("age");
                    String num=rs.getString("num");
                    s=new Student(newid,name,age,num);
                }
                jdbcUtil.close(con, pst, null);
                request.setAttribute("student", s);
                request.getRequestDispatcher("edit.jsp").forward(request, response);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            
        }
    
    }
    package com.servlet;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    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 com.jdbc.jdbcUtil;
    
    /**
     * Servlet implementation class editsuccess
     */
    @WebServlet("/editsuccess")
    public class editsuccess extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public editsuccess() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            try {
                Connection con=jdbcUtil.getcon();
                String sql="update stu set name=?,age=?,num=? where id=?";
                PreparedStatement pst=con.prepareStatement(sql);
                pst.setString(1, request.getParameter("name"));
                pst.setInt(2, Integer.parseInt(request.getParameter("age").toString()));
                pst.setString(3, request.getParameter("num"));
                pst.setInt(4, Integer.parseInt(request.getParameter("id").toString()));
                pst.executeUpdate();
                jdbcUtil.close(con, pst, null);
                response.sendRedirect(request.getContextPath()+"/list");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    Java 截取目标长度英文单词字符串 要求避免截断单词
    mysql报Fatal error encountered during command execution的解决办法
    PostgreSQL 学习01 Centos7.6安装PostgreSQL10
    Ubuntu14.04 + KinectV2驱动安装 以及 Ros接口(基于网上方法试错 改进版)
    传统定位方法简介--------里程计、IMU惯性传感器以及光电编码器等
    Vue.js 动态绑定class
    MongoDB数据导入导出成csv或者json
    exception 'yiiaseErrorException' with message 'Class 'MongoClient' not found'
    exception 'DOMException' with message 'Invalid Character Error' Php + Mongodb
    浅谈Angular的 $q, defer, promise
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14228658.html
Copyright © 2020-2023  润新知