天天看點

JSP連接配接Access資料庫

    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);

        }