Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

java - comparing values while inserting

I have taken name and password form user and compared it with my database if they match status is true if not its false.There is an additional column of role that the entered name can be user or admin if its admin I want some other page to open if it is user i want some other page.How should i do it. The following code checks admin since i have typed admin in the insert query what change or additional code i should write? There are 3 columns in my database(user,password,role).If the username entered is in same row as admin (some different page1.jsp should open) and if it is in same row as user (some different page2.jsp should open).

 String name=request.getParameter("name");
            String password=request.getParameter("password");
            boolean status=false;
    try{
        Connection con=ConnectionProvider.getCon();
        String sql="select * from roles where name='" + name + "' and pass='" + password + "' and role='admin'";
        Statement stmt =con.createStatement();

        ResultSet rs=stmt.executeQuery(sql);
        if(rs.next())
        {
            status=true;
        }
    }catch(Exception e){}

if(status){
                out.print("Welcome, "+name);
                HttpSession session=request.getSession();
                session.setAttribute("name",name);
                request.getRequestDispatcher("create.html").forward(request, response);
                 //request.getRequestDispatcher("department.html").forward(request, response);
            }
            else{
                out.print("Sorry, username or password error!");
                request.getRequestDispatcher("login.html").include(request, response);
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
String name=request.getParameter("name");
        String password=request.getParameter("password");
        boolean status=false;
String role = "";
try{
    Connection con=ConnectionProvider.getCon();
    String sql="select * from roles where name='" + name + "' and pass='" + password";
    Statement stmt =con.createStatement();
    role ="admin";
    ResultSet rs=stmt.executeQuery(sql);
    if(rs.next())
    {
        status=true;
        role=rs.getString("role");
    }
}catch(Exception e){}

if(status){
            out.print("Welcome, "+name);
            HttpSession session=request.getSession();
            session.setAttribute("name",name);
            if(role!=null && role.equals("admin") ){            
            request.getRequestDispatcher("create.html").forward(request, response);
             //request.getRequestDispatcher("department.html").forward(request, response);
            }else{
             request.getRequestDispatcher("   <SomeOther>.html").forward(request, response);
             //request.getRequestDispatcher("department.html").forward(request, response);
          }

        }
        else{
            out.print("Sorry, username or password error!");
            request.getRequestDispatcher("login.html").include(request, response);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...