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("涓嶇┖");
            }
        }
    }
  • 相关阅读:
    Windows 7/8/10 系统下Laravel框架的开发环境安装及部署详解(Vagrant + Homestead)
    简单易懂的laravel事件,这个功能非常的有用(监听事件,订阅者模式)
    搭建docker私有仓库
    CreatarGlobe实现多机立体显示方案(初稿)
    基于3D Vision眼镜的OSG立体显示 【转】
    一天干掉一只Monkey计划(一)——基本光照模型及RT后处理 【转】
    一天干掉一只Monkey计划(序)【转】
    RenderMonkey基本使用方法【转】
    怎样使用libcurl获取隐藏了文件后缀的url网络文件类型
    VR开发者必看:4大最为值得关注的内容平台【转】
  • 原文地址:https://www.cnblogs.com/520520520zl/p/12151676.html
Copyright © 2011-2022 走看看