weixin_45202083
17little
采纳率37.5%
2020-05-07 22:06

SpringMVC查找数据库时出现Index: 0, Size: 0问题

五月 07, 2020 9:40:09 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springmvc] in context with path [/lab17] threw exception [Request processing failed; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0] with root cause
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at cn.com.sise.dao.impl.TeacherDaoImpl.login(TeacherDaoImpl.java:54)
at cn.com.sise.service.impl.TeacherServiceImpl.login(TeacherServiceImpl.java:19)
at cn.com.sise.controller.TeacherController.login(TeacherController.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

五月 07, 2020 9:45:04 下午 org.apache.catalina.core.StandardContext reload
信息: Reloading Context with name [/lab17] has started
五月 07, 2020 9:45:04 下午 org.apache.catalina.core.ApplicationContext log
信息: Destroying Spring FrameworkServlet 'springmvc'
[DEBUG] 2020-05-07 21:45:04,423 org.springframework.web.context.support.XmlWebApplicationContext - Closing WebApplicationContext for namespace 'springmvc-servlet', started on Thu May 07 21:38:37 CST 2020
五月 07, 2020 9:45:04 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
警告: Web应用程序 [lab17] 注册了JDBC驱动程序 [com.mysql.jdbc.Driver],但在Web应用程序停止时无法注销它。 为防止内存泄漏,JDBC驱动程序已被强制取消注册。
五月 07, 2020 9:45:04 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
警告: The web application [lab17] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:502)
java.util.TimerThread.mainLoop(Timer.java:526)
java.util.TimerThread.run(Timer.java:505)
五月 07, 2020 9:45:11 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
五月 07, 2020 9:45:11 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。
五月 07, 2020 9:45:12 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring DispatcherServlet 'springmvc'

控制类:

@RequestMapping(value = "/doLogin",method = RequestMethod.POST)
public String login(String sno,String pwd,String role,Model model, HttpSession session) {
//管理员用户名和密码为学号
System.out.println("得到的账号密码:"+sno+pwd);
// 将用户对象添加到Session

        //角色为“Teacher”,管理员
        if(role.equals("t")){
            //在模型数据中添加教师登陆成功的提示信息
            session.setAttribute("USER_SESSION", sno);

            //在模型数据中添加教师的工号
            if(teaService.login(sno, pwd)){
                model.addAttribute("msg","管理员登录成功!");
                System.out.println("登录成功!");
                model.addAttribute("teano",sno);

        //跳转到adminInfo.jsp
          return "adminInfo";
        }else{
            //在模型数据中添加教师登陆失败的提示信息
            System.out.println("登录失败哦");
            model.addAttribute("msg", "工号或密码错误,请重新登录!");
            //返回到登陆页面
            return "login";
        }



                实现类:

                @Override
public Teacher login(String teano, String teapwd) {
    String sql="select * from t_teacher where teano=? and teapwd=?";
    //存放SQL语句的参数
    Object[] params =new Object[]{teano,teapwd}; 
    /*
     * 提供默认实现类 BeanPropertyRowMapper,使用此类要求数据表的列必须和java对象的属性对应
     *  BeanPropertyRowMapper将结果集通过java的反射机制映射到java对象中
     */
    RowMapper<Teacher> rowMapper = BeanPropertyRowMapper.newInstance(Teacher.class);
    //执行SQL,获取返回结果    return false;
    List<Teacher> stus=this.jdbcTemplate.query(sql, params, rowMapper);
    Teacher stu=null;
    if(stus!=null) stu=stus.get(0);
    return stu;
}



发现如果数据库为空的话,就不能再返回了....本来要实现的效果是 如果查询不到数据库的内容 就在msg插入账号密码错误的...怎么解决...
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • weixin_41935046 真空零点能 1年前

    if(stus!=null) stu=stus.get(0); 改成 if(stus!=null&&stus.size()>0) stu=stus.get(0);

    点赞 评论 复制链接分享