加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax+mysql

发布时间:2020-12-16 01:48:27 所属栏目:百科 来源:网络整理
导读:public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType( "text/html;charset=utf-8" ); response.setCharacterEncoding( "utf-8" ); PrintWriter out = response.ge
public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException {

        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        String id=request.getParameter("username");
        String password=request.getParameter("password");
        
        out.println("id = " +id);
        
        //Servlet操作数据库和普通java类一样
        
        Connection ct= null;
        PreparedStatement ps =null;
        ResultSet rs = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接
            ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","1");
            //3.创建PreparedStatment 用于传送sql查询语句
            ps=(PreparedStatement) ct.prepareStatement("select * from users where id =? and password=?");
            //给?赋值
            ps.setObject(1,id);
            ps.setObject(2,password);
            
            //4.执行操作
            rs= ps.executeQuery();
            //5.根据结果做处理
            if(rs.next())
            {//合法
                request.getRequestDispatcher("/MainFrame").forward(request,response);
            }else
            {
                request.setAttribute("error","用户名 或者 密码错误!");
                request.getRequestDispatcher("/LoginServlet").forward(request,response);
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }finally
        {
            //关闭资源
            if(rs!=null)
            {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                rs=null;
            }
            if(ps!=null)
            {
                try {
                    ps.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                ps=null;
            }
            if(ct!=null)
            {
                try {
                    ct.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                ct=null;
            }
        }
        
        
        //out.println("username"+username);
        
        

    }

    public void doPost(HttpServletRequest request,IOException {

        this.doGet(request,response);

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读