• 公文流转系统部分之分角色登录


    这一部分我主要实现了多用户登录,主要是设定一个权限,通过比较权限实现不同用户的登录。

    dbutil层

    package com.fin.util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class BaseConnection {
         public static Connection getConnection(){//用这个方法获取mysql的连接
             Connection conn=null;
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost:3306/zsgc1?characterEncoding=utf8&useSSL=true";
             String user = "root";
             String password = "123456";
             try{
                 Class.forName(driver);//加载驱动类
                 conn=DriverManager.   
                         getConnection(url,user,password);//(url数据库的IP地址,user数据库用户名,password数据库密码)
             }catch(Exception e){
                 e.printStackTrace();
             }
             return conn;
         }
         
         public static void close (Statement state, Connection conn) {
                if (state != null) {
                    try {
                        state.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            public static void close (ResultSet rs, Statement state, Connection conn) {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                
                if (state != null) {
                    try {
                        state.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
    
    
        public static void main(String[] args) {
            System.out.println("连接成功");
        }
    
    }
    View Code

    Javabean

    package com.xk.bean;
    
    public class user {
         private String username;
         private String password;
         private int id;
         private String id1;
        
    
        
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getId1() {
            return id1;
        }
        public void setId1(String id1) {
            this.id1 = id1;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        
        
        
        
        public user(int id, String username, String password,String id1) {
            // TODO 自动生成的构造函数存根
            this.id = id;
            this.username = username;
            this.password = password;
            this.id1 = id1;
        }
    
    
        
        public user(String username, String password,String id1) {
            // TODO 自动生成的构造函数存根
            this.username = username;
            this.password = password;
            this.id1 = id1;
        }
    }
    View Code

    dao

    package com.xk.dao;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    import com.xk.bean.user;
    import com.xk.bean.user1;
    import com.xk.bean.user2;
    import com.xk.bean.user3;
    import com.xk.bean.Page;
    import com.xk.dbutil.Dbutil;;
    
    public class Dao {
        public String dopost(user dl) {
            String i="0";
            String sql="select * from admin where username = '"+dl.getUsername()+"'";
            Connection conn = Dbutil.getConn();
            Statement state = null;
            ResultSet rs = null;
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while(rs.next()){
                String password1 = rs.getString("password");
                String password2 = dl.getPassword();
                if(dl.getPassword().equals(password1)) {
                    i=rs.getString("id1");
                }
                break;
               }
            }catch (Exception e) {
                e.printStackTrace();
            } finally {
                Dbutil.close(rs,state, conn);
            }
            return i;
        }
        
        
    }
    View Code

    servlet

    package com.xk.servlet;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    //import javax.smartcardio.ResponseAPDU;
    import com.xk.bean.user;
    import com.xk.bean.user1;
    import com.xk.bean.user2;
    import com.xk.bean.user3;
    import com.xk.dao.Dao;
    /**
     * Servlet implementation class CourseServlet
     */
    @WebServlet("/Servlet")
    
    public class Servlet extends HttpServlet {
         private static final long serialVersionUID = 1L;
         
            
            public Servlet() {
                super();
                
            }
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub
                //response.getWriter().append("Served at: ").append(request.getContextPath());
                
                response.setContentType("text/html;charset=UTF-8");
                request.setCharacterEncoding("UTF-8");
                
                String method = request.getParameter("method");
                 if(method.equals("dopost"))
                {
                    dopost(request,response);
                    
                }
                
                
            }
            
            private void dopost(HttpServletRequest requst, HttpServletResponse response) throws IOException, ServletException{
                requst.setCharacterEncoding("utf-8");
                String username = requst.getParameter("username");
                String password = requst.getParameter("password");
                String id1 = requst.getParameter("id1");
                System.out.println(username);
                Dao dao=new Dao();
                user course = new user(username,password,id1);
                String id=dao.dopost(course);
                if(id.equals("0")) {
                    requst.setAttribute("message", "登录失败!");
                    requst.getRequestDispatcher("index.jsp").forward(requst,response);
                }
                else if(id.equals("1")) {
                    requst.setAttribute("message", "登陆成功!");
                    requst.getRequestDispatcher("manager.jsp").forward(requst,response);
                }
                else if(id.equals("2")) {
                    requst.setAttribute("message", "登陆成功!");
                    requst.getRequestDispatcher("teacher.jsp").forward(requst,response);
                }
                else if(id.equals("3")) {
                    requst.setAttribute("message", "登陆成功!");
                    requst.getRequestDispatcher("student.jsp").forward(requst,response);
                }
                
            }
          
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub
                doGet(request, response);
                
            }
    
    
    }
    View Code
  • 相关阅读:
    使用 ALinq 支持多种数据库
    jsData 使用教程(-) 加载数据
    java中的标准I/O流与文件1(J2SE入门16)
    java中的反射(J2SE入门20)
    java中的java5.0的新特性2(J2SE入门22)
    java中的网络入门2(J2SE入门19)
    java中的标准I/O流与文件2(J2SE入门17)
    java中的Java5.0 的注释 (Annotation)、多线程包1(J2SE入门25)
    java中的网络入门1(J2SE入门18)
    java中的java5.0的泛型1(J2SE入门23)
  • 原文地址:https://www.cnblogs.com/ljpljm/p/12158809.html
Copyright © 2020-2023  润新知