日期:2014-05-20  浏览次数:20780 次

spring+JDBC
Struts+Spring 做简单的注册登录

----------------------------------

public boolean LoginDao(LoginForm loginform){
boolean flag=false;
UserInfo info=null;



info=(UserInfo)this.getJdbcTemplate().queryForObject("select * from userInfo where userName=? and userPwd=?",new String[]{loginform.getUserName(),loginform.getUserPwd()}, new RowMapper(){

public Object mapRow(ResultSet rs, int arg1) throws SQLException {

UserInfo userinfo=new UserInfo();

userinfo.setUserName(rs.getString("userName"));
userinfo.setUserPwd(rs.getString("userPwd"));
return userinfo;
}
});



String userName=info.getUserName();
String userPwd=info.getUserPwd();

if(userName.equals(loginform.getUserName()) && userPwd.equals(loginform.getUserPwd())){
flag=true;
}

return flag;
}


如果用户名和密码不正确正确就this.getJdbcTemplate().queryForObject 该方法就是空(异常)


------------------------------

严重: Servlet.service() for servlet action 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:669)
at dao.BaseDao.LoginDao(BaseDao.java:23)
at action.LoginAction.execute(LoginAction.java:31)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)



------解决方案--------------------
queryForObject无记录是抛出异常,就是你贴的那个异常

所以要用try,或者用queryForList