weixin_49095187 2021-06-13 14:34 采纳率: 33.3%
浏览 39
已结题

这个MVC我用学号查询数据库用户为什么会报空指针异常?

这是查询代码:

//根据用户ID查找指定用户
public Student studentByID(String sid){
    Student student =new Student();
    Statement stmt3=null;
    ResultSet rs=null;
    Connection con2=null;
    try{
        stmt3=con2.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        rs=stmt3.executeQuery("select * from student where Sid='"+sid+"'");
        if (rs.next()){
            student.setSid(rs.getString(1));
            student.setName(rs.getString(2));
            student.setClasses(rs.getInt(3));
            student.setSex(rs.getString(4));
            student.setEmail(rs.getString(5));
            student.setPolicalstatus(rs.getString(6));
            student.setPassword(rs.getString(7));
            student.setAge(rs.getInt(8));
            student.setRole(rs.getString(9));
        }
        rs.close();
        stmt3.close();
        con2.close();
    }
    catch (SQLException ex){
        ex.printStackTrace();
    }
    return student;
}

//这是登录页的代码:

String name=req.getParameter("names");
    String password=req.getParameter("passwords");
    String sid=req.getParameter("sid");
    String role="";
    //调用用户模型,获取用户角色
    studentDAO studentDAO = new dbconnection();
    student = studentDAO.studentByID(sid);
    req.setAttribute("student",student);
    if(password.equals(studentDAO.studentByID(sid).getPassword())){
        role=studentDAO.validate(name,password);
        if(role.equals("普通用户")){
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("userpage.jsp");
            requestDispatcher.forward(req,resp);
        }
        else if (role.equals("管理员")){
            RequestDispatcher  requestDispatcher =req.getRequestDispatcher("managerpage.jsp");
            requestDispatcher.forward(req,resp);
        }
        else{
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("load.jsp");
            requestDispatcher.forward(req,resp);
        }
    }
    else{
        System.out.println("您的密码有误!");
        RequestDispatcher requestDispatcher=req.getRequestDispatcher("load.jsp");
        requestDispatcher.forward(req,resp);
    }
}
  • 写回答

5条回答 默认 最新

  • 关注
     Connection con2=null;

    con2没有创建对象,建立连接,添加代码如下:

    Class.forName(driver);

    con2 = DriverManager.getConnection(url,"sa","123456");

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月30日

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的