5

帮忙看一下为什么修改不了数据库信息

Servlet代码

@WebServlet("/UpdateStudent")
public class UpdateStudent extends HttpServlet {
    private static final long serialVersionUID = 1L;



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if(idStr != null && !idStr.equals("")){
            int id = Integer.valueOf(idStr);
            AdminDao dao = new AdminDao();
            Student stu = dao.selectStudentById(id);
            request.setAttribute("stu", stu);
        }
        request.getRequestDispatcher("/UpdateStudent.jsp").forward(request, response);

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String sname = request.getParameter("sname");
        String password = request.getParameter("password");
        String sno = request.getParameter("sno");
        String sex = request.getParameter("sex");
        String major = request.getParameter("major");
        String grade = request.getParameter("grade");
        Student student = new Student();


        student.setSname(new String(sname.getBytes("ISO-8859-1"),"UTF-8"));
        student.setPassword(password);
        student.setSno(sno);
        student.setSex(new String(sex.getBytes("ISO-8859-1"),"UTF-8"));
        student.setMajor(new String(major.getBytes("ISO-8859-1"),"UTF-8"));
        student.setGrade(new String(grade.getBytes("ISO-8859-1"),"UTF-8"));
        AdminDao dao = new AdminDao();
        dao.UpdateStudent(student);
        request.getRequestDispatcher("/Information").forward(request, response);

}

Dao代码

public void UpdateStudent(Student stu) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/appoi";
            Connection con = DriverManager.getConnection(url, "root", "123348");

            String sql = "update student set sname=?,password =?,sno=?,sex=?,major=?,grade=? where id=?";
            PreparedStatement cmd = con.prepareStatement(sql);
            cmd.setInt(1, stu.getId());
            cmd.setString(2, stu.getSname());
            cmd.setString(3, stu.getPassword());
            cmd.setString(4, stu.getSno());
            cmd.setString(5, stu.getSex());
            cmd.setString(6, stu.getMajor());
            cmd.setString(7, stu.getGrade());
            cmd.executeUpdate();

            con.close();

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //根据学生ID进行查询
    public Student selectStudentById(int id){
        Student stu = null;  
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/appoi";
            Connection con = DriverManager.getConnection(url, "root", "123348");

            String sql  = "select * from student where id = "+id;

            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()) {
                stu = new Student();
                stu.setId(rst.getInt("id"));
                stu.setSname(rst.getString("sname"));
                stu.setPassword(rst.getString("password"));
                stu.setSno(rst.getString("sno"));
                stu.setSex(rst.getString("sex"));
                stu.setMajor(rst.getString("major"));
                stu.setGrade(rst.getString("grade"));
                }
            con.close();
            }
         catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            return stu; //返回
        }

jsp的form表单

<form action="UpdateStudent?id=<%=stu.getId() %>"   method="post">
<table>
<tr><td>学生编号:<input type="text" name="id" value="${stu.id }" /></td></tr>
<tr><td>登录名:<input type="text" name="sname" value="${stu.sname }"/></td></tr>
<tr><td>密码:<input type="password" name="password" value="${stu.password }"/></td></tr>
<tr><td>学号:<input type="text" name="sno" value="${stu.sno }"/></td></tr>
<tr><td>性别:<input type="text" name="sex" value="${stu.sex }"/></td></tr>
<tr><td>专业:<input type="text" name="major" value="${stu.major }"/></td></tr>
<tr><td>班级:<input type="text" name="grade" value="${stu.grade }"/></td></tr>
<tr><td ><input type="submit"  value="提交"/></td></tr>

<tr><td><button type="submit" value="返回"/><a href="${pageContext.request.contextPath}/Information">返回</a></button></td></tr>
</table>
</form>
查看全部
qq_38104700
qq_38104700
2019/03/05 17:37
  • java
  • tomcat
  • java-ee
  • eclipse
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复