第七周作业

package org.mbw.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.mbw.entity.Login;

//模型层:用于处理登录(查询数据)
public class LoginDao {
        public static int login(Login Login) {//登录
            //boolean flag = false;
            int flag = -1 ;//-1:系统异常 0:用户密码或账号错误 1:正常
            int result = -1;
            String URL = "jdbc:mysql://localhost:3306/move?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
            String USERNAME = "root";
            String PWD = "123456";
            
            Connection connection = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(URL, USERNAME, PWD);
                
                String sql = "select count(*) from user where uname = ? and pwsd = ?";
                ps = connection.prepareStatement(sql);
                ps.setString(1, Login.getUname());
                ps.setString(2, Login.getPwsd());
                rs = ps.executeQuery();
                if(rs.next()) {
                    result = rs.getInt(1);
                }
                if(result>0) {//登录成功
                    return 1;
                }else {
                  return 0;//登录失败(用户名或密码有误)
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return -1;//登录异常
            }catch(SQLException e) {
                e.printStackTrace();
                return -1;
            }catch(Exception e) {
                e.printStackTrace();
                return -1;
            }finally {
                try {
                if(rs!=null) rs.close();
                if(ps!=null) rs.close();
                if(connection!=null) rs.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
}

package org.mbw.entity;

public class Login {
       private int id;
       private String uname;
       private String pwsd;
       
   public Login() {
}
   
   public Login(String uname, String pwsd) {
        this.uname = uname;
        this.pwsd = pwsd;
    }
    public Login(int id, String uname, String pwsd) {
        this.id = id;
        this.uname = uname;
        this.pwsd = pwsd;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPwsd() {
        return pwsd;
    }
    public void setPwsd(String pwsd) {
        this.pwsd = pwsd;
    }
       
       
}


package org.mbw.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mbw.dao.LoginDao;
import org.mbw.entity.Login;

//控制器层:接受view请求,并分发给Model层处理
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //处理登录请求
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("uname");
        String pwd = request.getParameter("upwsd");
        Login login = new Login(name,pwd);//用户名,密码
        
        //调用模型层的登录功能
        int result = LoginDao.login(login);
        if(result>0) {
            response.sendRedirect("Welcome.jsp");
        }else {
            response.sendRedirect("login.jsp");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 

posted @ 2021-04-18 16:04  董文博。  阅读(44)  评论(0编辑  收藏  举报