qq_38104700 2019-03-05 17:37 采纳率: 75%
浏览 695
已采纳

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

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>
  • 写回答

2条回答 默认 最新

  • ThompJie 2019-03-06 09:34
    关注

    cmd.setInt(1, stu.getId());应该改为cmd.setInt(7, stu.getId());就对了,注意sql语句注入顺序

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误