当前位置:编程学习 > JAVA >>

求帮忙.............管理员身份验证 可是现在怎么也登不进去 到底是哪里出错了

代码 我贴这  各位大侠帮帮忙


-------------------
输出的exception:

获取的查询字符串:login
验证结果的ret值:2
---------------------

package com.wgh.dao;

import com.wgh.actionForm.ManagerForm;
import com.wgh.core.ConnDB;
import java.sql.*;
import java.util.*;

public class ManagerDAO {
private ConnDB conn=new ConnDB();
    //管理员身份验证
    public int checkManager(ManagerForm managerForm) {
        int flag = 1;
        String sql = "SELECT * FROM tb_manager where name='" +
                     managerForm.getName() + "'";
        ResultSet rs = conn.executeQuery(sql);
        try {
            if (rs.next()) {
                String pwd = managerForm.getPwd();
                if (pwd.equals(rs.getString(3))) {
                    flag = rs.getByte(4); //获取状态信息
                    rs.last();
                    int rowSum = rs.getRow(); //获取记录总数
                    rs.first();
                    if (rowSum != 1) {
                        flag = 2;
                        System.out.print("获取的row的值:" + sql + rowSum);
                    }
                } else {
                    flag = 2;
                }
            }else{
                flag = 2;
            }
        } catch (SQLException ex) {
            flag = 2;
        }
        return flag;
    }
    //添加数据
    public int insert(ManagerForm managerForm) {
        String sql1="SELECT * FROM tb_manager WHERE name='"+managerForm.getName()+"'";
        ResultSet rs = conn.executeQuery(sql1);
        String sql = "";
        int falg = 0;
            try {
                if (rs.next()) {
                    falg=2;
                } else {
                    sql = "INSERT INTO tb_manager (name,pwd) values('" +
                                 managerForm.getName() + "','" +
                                 managerForm.getPwd() +
                                 "')";
                    falg = conn.executeUpdate(sql);
                    System.out.println("添加管理员信息的SQL:" + sql);
                    conn.close();
                }
            } catch (SQLException ex) {
                falg=0;
            }
        return falg;
    }
    //查询方法
    public List query(ManagerForm managerForm) {
     List managerList = new ArrayList();
        ManagerForm managerForm1 = null;
        String sql="";
        if(managerForm==null){
            sql = "SELECT * FROM tb_manager WHERE state=0";
        }else{
         sql = "SELECT * FROM tb_manager WHERE name='" +managerForm.getName() + "'";
        }
        ResultSet rs = conn.executeQuery(sql);
        try {
            while (rs.next()) {
                managerForm1 = new ManagerForm();
                managerForm1.setID(rs.getInt(1));
                managerForm1.setName(rs.getString(2));
                managerForm1.setPwd(rs.getString(3));
                managerForm1.setState(rs.getByte(4));
                managerList.add(managerForm1);
            }
        } catch (SQLException ex) {}
        return managerList;
    }
    //修改管理员密码
    public int updatePwd(ManagerForm managerForm){
        String sql="UPDATE tb_manager SET pwd='"+managerForm.getPwd()+"' where name='"+managerForm.getName()+"'";
        int ret=conn.executeUpdate(sql);
        System.out.println("修改管理员密码时的SQL:"+sql);
        conn.close();
        return ret;
    }

//    删除数据
        public int delete(ManagerForm managerForm) {
            String sql = "DELETE FROM tb_manager where id=" + managerForm.getID() +"";
            int flag = conn.executeUpdate(sql);
            conn.close();
            return flag;
        }
}
--------------------------------------------------------------------------------
package com.wgh.action;

import org.apache.struts.action.*;

import com.wgh.actionForm.ManagerForm;
import com.wgh.core.ChStr;
import com.wgh.dao.ManagerDAO;

import javax.servlet.http.*;

public class Manager extends Action {
    private ManagerDAO managerDAO = null;
    private ChStr chStr=new ChStr();
    public Manager() {
        this.managerDAO = new ManagerDAO();
    }
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
        String action = request.getParameter("action");
        System.out.println("获取的查询字符串:" + action);
        if (action == null || "".equals(action)) {
            return mapping.findForward("error");
        }else if ("login".equals(action)) {
            return managerLogin(mapping, form, request,response);

}else if ("managerQuery".equals(action)) {
            return managerQuery(mapping, form, request,response);

}else if("managerAdd".equals(action)){
 return managerAdd(mapping, form, request,response);
}else if("managerDel".equals(action)){
return managerDel(mapping, form, request,response);
} else if("queryPWD".equals(action)){
                return pwdQuery(mapping, form, request,response);
        }else if("modifypwd".equals(action)){
            return modifypwd(mapping, form, request,response);
        }
request.setAttribute("error", "操作失败!");
return mapping.findForward("error");
}

    //管理员身份验证
    public ActionForward managerLogin(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        ManagerForm managerForm = (ManagerForm) form;
        managerForm.setName(chStr.toChinese(managerForm.getName()));
        managerForm.setPwd(managerForm.getPwd());
        int ret = managerDAO.checkManager(managerForm);
        System.out.print("验证结果ret的值:" + ret);
        if (ret == 2) {
            request.setAttribute("error","您输入的管理员名称或密码错误!");
            return mapping.findForward("error");
        }else{
            HttpSession session=request.getSession();
            session.setAttribute("manager",managerForm.getName());
            session.setAttribute("purview", String.valueOf(ret));
            return mapping.findForward("managerLoginok");
        }
    }
    //查询管理员信息
    private ActionForward managerQuery(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        request.setAttribute("managerQuery", managerDAO.query(null));
        return mapping.findForward("managerQuery");
    }

    //添加管理员信息
    private ActionForward managerAdd(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        ManagerForm managerForm = (ManagerForm) form;
        managerForm.setName(chStr.toChinese(managerForm.getName()));
        managerForm.setPwd(managerForm.getPwd());
        int ret = managerDAO.insert(managerForm);
        System.out.println("返回值ret:"+ret);
        if (ret == 1) {
            return mapping.findForward("managerAdd");
        } else if(ret==2){
            request.setAttribute("error","该管理员信息已经添加!");
            return mapping.findForward("error");
        }else {
            request.setAttribute("error","添加管理员信息失败!");
            return mapping.findForward("error");
        }
    }
    //修改密码时查询
    private ActionForward pwdQuery(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        ManagerForm managerForm = (ManagerForm) form;
        HttpSession session=request.getSession();
        String manager= (String) session.getAttribute("manager"); //获取管理员名称
        managerForm.setName(manager);
        System.out.print("查询到的manager:"+manager);
        request.setAttribute("pwdQueryif",
                                    managerDAO.query(managerForm));
        return mapping.findForward("pwdQueryModify");
    }
    //修改管理员密码
    private ActionForward modifypwd(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response){
        ManagerForm managerForm=(ManagerForm) form;
        managerForm.setName(managerForm.getName());
        managerForm.setPwd(managerForm.getPwd());
        int ret=managerDAO.updatePwd(managerForm);
        if(ret==0){
            request.setAttribute("error","更改口令失败!");
            return mapping.findForward("error");
        }else{
            return mapping.findForward("pwdModify");
        }
    }
    //删除管理员信息
    private ActionForward managerDel(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        ManagerForm managerForm = (ManagerForm) form;
        managerForm.setID(Integer.parseInt(request.getParameter("id")));
        int ret = managerDAO.delete(managerForm);
        if (ret == 0) {
            request.setAttribute("error","删除管理员信息失败!");
            return mapping.findForward("error");
        } else {
            return mapping.findForward("managerDel");
        }
    }

}
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,