zoukankan      html  css  js  c++  java
  • JAVAWEB登陆跳转后台

    package dao;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import entry.user;
    import util.DBUtil;
    
    @SuppressWarnings("unused")
    public class userDao {
        //
        
            
            
            public static boolean id(String id,String pwd) {
                boolean f = false;
                String sql = "select * from user3 where id = '" + id + "' and password = '"+pwd+"'";
                //
                Connection conn = DBUtil.getConn();
                Statement state = null;
                ResultSet rs = null;       
                try {
                    state = conn.createStatement();
                    rs = state.executeQuery(sql);
                    if (rs.next()) {
                        f = true;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    DBUtil.close(rs, state, conn);
                }
                return f;
            }
    }
    package entry;
    
    public class user {
    private String id;
    private String password;
    
    
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    public user(){}
    public user( String password) {
        
        this.password = password;
    };
    public user(String id, String password) {
        super();
        this.id = id;
        this.password = password;
        
    }
    }
    
    package servlet;
    
    import java.io.IOException;
    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 dao.userDao;
    
    
    /**
     * Servlet implementation class AddServlet
     */
    @WebServlet("/AddServlet")
    public class AddServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
       
            
            
            protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                 request.setCharacterEncoding("utf-8");
                 //鑾峰彇鏁版嵁
                 String id = request.getParameter("id");
                 String password = request.getParameter("password");
                 
               
                 
                
                  System.out.println(id);
                 System.out.println(password);
                 if(userDao.id(id,password)) {
                     request.setAttribute("message", "登录成功");
                     request.getRequestDispatcher("chenggong.jsp").forward(request,response);
                 } else {
                     request.setAttribute("message", "登录失败");
                     request.getRequestDispatcher("houtai.jsp").forward(request,response);
                 }
            }
            
            
            
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
                doGet(request, response);
            }
    
        
    
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
    
    
    }
    package util;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    /*
     * 鏁版嵁搴撹繛鎺ュ伐鍏�
     */
    public class DBUtil {
         
        public static String db_url = "jdbc:mysql://localhost:3306/user?useSSL=false";
        public static String db_user = "root";
        public static String db_pass = "root";
         
        public static Connection getConn () {
            Connection conn = null;
             
            try {
                Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
             
            return conn;
        }
         
        /*10鍏抽棴杩炴帴*/
        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) throws SQLException {
            Connection conn = getConn();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql ="select * from user";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
                System.out.println("绌�");
            }else{
                System.out.println("涓嶇┖");
            }
        }
    }
  • 相关阅读:
    The Future of Middleware and the BizTalk Roadmap
    FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
    Strips illegal Xml characters
    luogu P2280 激光炸弹(二维前缀和)
    luogu P2704 炮兵阵地(经典状态压缩DP)
    SP1716 GSS3 Can you answer these queries III (线段树维护最大连续子段和)
    二分图判定、匹配问题
    C++语法综合 | 基于char*设计一个字符串类MyString
    luogu P1044 火车进出栈问题(Catalan数)
    C++设计模式 | 三种设计模式基础
  • 原文地址:https://www.cnblogs.com/520520520zl/p/12151676.html
Copyright © 2011-2022 走看看