为什么在ACTION 中调用userbean 的chklog函数后,无法返回数据?
数据库跟踪显示SQL执行有记录,返回值是1,但是
if (chk.equals(1)) {
return (mapping.findForward("ok"));
}
else { return (mapping.findForward("no")); }
转发全是"no"值,代码如下,只保留return (mapping.findForward("ok"));运行正常。 ---ACTION-------------
package dev;import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.lang.*;public class loginAtion
extends Action {
private Integer chk;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
LoginForm f = (LoginForm)form;
try {
UserBean b = new UserBean();
chk =b.chklog(f.getlg());
}
catch (Exception e)
{
e.printStackTrace();
} request.setAttribute("lg",f.getlg());
if (chk.equals(1)) {
return (mapping.findForward("ok"));
}
else { return (mapping.findForward("no")); }
} }---java-----
package dev;
import java.sql.*;public class UserBean {
private Connection con;
private ResultSet result;
private Statement stmt;
private String sql;public UserBean()throws Exception
{
this.con=DBPool.getPool().getConnection();
} public int chklog(login lg)throws Exception
{
int ret=0;
try {
stmt = con.createStatement();
sql = "select count(*)as ti from tab_user where uname='" + lg.getUserid() +
"' and upass='" + lg.getPassword() + "'";
result = stmt.executeQuery(sql);
if (!result.next()) { ret = result.getInt("ti"); }
}
finally {
if(result!=null) try{ result.close(); } catch(Exception ex){}
if(stmt!=null) try{ stmt.close(); } catch(Exception ex){}
} return ret;}}
数据库跟踪显示SQL执行有记录,返回值是1,但是
if (chk.equals(1)) {
return (mapping.findForward("ok"));
}
else { return (mapping.findForward("no")); }
转发全是"no"值,代码如下,只保留return (mapping.findForward("ok"));运行正常。 ---ACTION-------------
package dev;import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.lang.*;public class loginAtion
extends Action {
private Integer chk;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
LoginForm f = (LoginForm)form;
try {
UserBean b = new UserBean();
chk =b.chklog(f.getlg());
}
catch (Exception e)
{
e.printStackTrace();
} request.setAttribute("lg",f.getlg());
if (chk.equals(1)) {
return (mapping.findForward("ok"));
}
else { return (mapping.findForward("no")); }
} }---java-----
package dev;
import java.sql.*;public class UserBean {
private Connection con;
private ResultSet result;
private Statement stmt;
private String sql;public UserBean()throws Exception
{
this.con=DBPool.getPool().getConnection();
} public int chklog(login lg)throws Exception
{
int ret=0;
try {
stmt = con.createStatement();
sql = "select count(*)as ti from tab_user where uname='" + lg.getUserid() +
"' and upass='" + lg.getPassword() + "'";
result = stmt.executeQuery(sql);
if (!result.next()) { ret = result.getInt("ti"); }
}
finally {
if(result!=null) try{ result.close(); } catch(Exception ex){}
if(stmt!=null) try{ stmt.close(); } catch(Exception ex){}
} return ret;}}
要不把bean里面的代码打上一堆断点,看ret的值变化情况
return (mapping.findForward("ok"));
} 试下看........
太粗心大意了,记录是否存在判断写的有问题,把
if (!result.next())
改为if (result.next())
就正确了。