JSP连接Access数据库

本文详细阐述了如何通过Java的HTTP请求参数获取用户名和密码,并利用ODBC驱动与Mdb数据库建立连接,验证用户登录信息的有效性。如果验证失败,则会重定向至登录页面;反之,将用户名记录至session并跳转至主页面。
    String sName = request.getParameter("userName");
    String pwd = request.getParameter("password");
   
    //连接mdb数据库,验证用户名密码是否正确
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(ClassNotFoundException e){
            out.print("driver:"+e);
        }
        try{
            String url="jdbc:odbc:jspdata";
            Connection conn = DriverManager.getConnection(url);
            Statement stmt = conn.createStatement();
            String sql = "select * from userTable where userName='"+sName +"' and userPwd = '"+pwd+"'";
            ResultSet rs = stmt.executeQuery(sql);
            if(!rs.next()){
                //查不出结果则返回到登录页面
                response.sendRedirect("index.jsp");
            }
           else{
                //将用户名记录到session中
                session.setAttribute("userName", sName);
                
                //登录主页面
                request.getRequestDispatcher("main.jsp").forward(request, response);
               
           }
        }
        catch(Exception e){
            out.print(e);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值