JSP连接Access数据库

来源:互联网 发布:软件方案怎么写 编辑:程序博客网 时间:2024/05/17 07:05
    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);
        }