zoukankan      html  css  js  c++  java
  • jsp第六周作业

    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);
        }
    
    }
  • 相关阅读:
    android ContentObserver
    3 个简单、优秀的 Linux 网络监视器
    使用 sar 和 kSar 来发现 Linux 性能瓶颈
    4 个拥有绝佳命令行界面的终端程序
    4 个用于构建优秀的命令行用户界面的 Python 库
    理解 Linux 的平均负载和性能监控
    安装matplotlib
    面向系统管理员的网络管理指南
    使用 Nmon 监控 Linux 的系统性能
    linux smem 查看各进程使用memory情况
  • 原文地址:https://www.cnblogs.com/mbw123/p/14673671.html
Copyright © 2011-2022 走看看