Spring jdbcTemplate结果集null错误

时间:2015-10-07 06:19:47

标签: spring jsp spring-data resultset jdbctemplate

我正在使用spring Data Access设计登录表单。当用户输入正确的用户名和密码时,应用程序运行良好,当他提供错误的用户名和密码时,它没有进入所需的页面。这是我的代码:

的login.jsp

<body>
    <h1>Login</h1>
    <form action="Registration" method="POST">
        <table>
            <tr>
                <td>Username</td>
                <td><input type="text" name="username"></td>
            </tr>

            <tr>
                <td>Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" name="login" value="Login"></td>
            </tr>
        </table>
    </form>
</body>

针对home.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Login Success</h1>
</body>
</html>

invalid.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Wrong UserCredentials</h1>
</body>
</html>

EmployeeDao

public class EmployeeDao {

JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public EmployeeDao(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public EmployeeDao()
{

}

public Register getRegisterationids(String uname,String password)
{
    String query="select * from registeration where uname=? and password=?";
    return jdbcTemplate.queryForObject(query, new Object[] {uname,password},new Mapper());


}

private static final class Mapper implements RowMapper<Register>
{

    @Override
    public Register mapRow(ResultSet rs, int i) throws SQLException {
        Register r=new Register();
        r.setUname(rs.getString("uname"));
        r.setPassword(rs.getString("password"));
        return r;
    }

}
}

Registration.java

 if(request.getParameter("login")!=null)
    {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        /*Register rdao=(Register) ac.getBean("Register");
        rdao.setUname(username);
        rdao.setPassword(password);*/
        EmployeeDao regisdao=(EmployeeDao)ac.getBean("EmployeeDao"); 

        String uname1=regisdao.getRegisterationids(username, password).getUname();
        String password1=regisdao.getRegisterationids(username, password).getPassword();

        if(username.equals(uname1)&&password.equals(password1))
        {
            RequestDispatcher rd=request.getRequestDispatcher("/home.jsp");
            rd.forward(request, response);
        }

        else
        {
            RequestDispatcher rd=request.getRequestDispatcher("/invalid.jsp");
            rd.forward(request, response);
        }     
    }

当用户输入正确的用户和密码时,它会进入主页。当用户输入错误的用户名和密码时会抛出错误,这是错误。

错误

StandardWrapperValve[Registration]: Servlet.service() for servlet Registration threw exception
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
at       org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:804)
at com.Dao.EmployeeDao.getRegisterationids(EmployeeDao.java:52)
at com.Servlets.Registration.processRequest(Registration.java:99)
at com.Servlets.Registration.doPost(Registration.java:147)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:295)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)

]]

它显示结果集为空且预期为最小1 ..任何人都可以帮助解决这个问题..如果用户应该将错误的凭据用于invalid.jsp ......

0 个答案:

没有答案