当我要使用hibernate在servlet中获取表数据时,会发生异常

时间:2017-10-19 11:06:15

标签: mysql hibernate servlets

selectStudent servlet

/**
 * @see HttpServlet#HttpServlet()
 */
public SelectStudent() {
    super();
    // TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub

    Configuration cf=new Configuration();
    cf.configure("hibernate.cfg.xml");

    sf=cf.buildSessionFactory();

}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

//RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
     // rd.include(request, response);
      PrintWriter out = response.getWriter();
      Session s=sf.openSession();
      Object o = s.get(Student.class, 5);//Here i selecting 5th record from my table
      Student st1=(Student)o;   
        out.println(st1.getId()+"<br>");
        out.println(st1.getName()+"<br>");
        out.println(st1.getEmail()+"<br>");
        out.println(st1.getMarks());

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

学生表

    Id  Name    Email   Marks
    2   bjhbs   nsj     123
    3   hbjhsb  njdkn   12
    5   tofik   tofikstamboli1@gmail.com    123

enter image description here

发生

错误: Hibernate:选择student0_.Id为Id1_0_0_,student0_.Name为Name2_0_0_,student0_.Email为Email3_0_0_,student0_.Marks为Marks4_0_0_来自Student student0_,其中student0_.Id =?

2017年10月19日下午3:59:04 org.apache.catalina.core.StandardWrapperValve调用 严重:Servlet [beans.SelectStudent]的Servlet.service()在路径[/ CRUD]的上下文中引发了异常 显示java.lang.NullPointerException

0 个答案:

没有答案