dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Servlet Does not write to database

  1. #1
    Join Date
    Aug 2012
    Location
    Bowie Maryland
    Posts
    1

    Servlet Does not write to database

    I am pretty new to Java Servlets and I Need to be pointed to the right direction. I am writing a login servlet which somehow does not appear to be querying the database.

    Please some directions as to what I am doing wrong.

    Thanks
    Jonas

    package WEBp;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;


    public class AuthenticateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    }


    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher dispatcher ;
    PrintWriter out = response.getWriter();
    HttpSession UserSession = request.getSession();
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String category = request.getParameter("category");
    try{

    response.setContentType("text/html");

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://192.168.1.22/StudentGradeManagement", "jokwara", "chi4me");
    Statement statement =con.createStatement();
    ResultSet results = statement.executeQuery("select username, password, category from authenticate where username =' "+username+" ' ");

    while(results.next()){
    String realUser = results.getString(username);
    String realPassword = results.getString(password);
    String realCategory = results.getString(category);


    if( ( username.equals(realUser) ) || ( realCategory.equals("student") ) || (password.equals(realPassword)) ) {
    UserSession.setAttribute("realUser", realUser);
    response.sendRedirect("studentView.jsp");
    dispatcher = request.getRequestDispatcher("studentView.jsp");
    dispatcher.forward(request, response);

    }
    else if (( username.equals(realUser) ) || (password.equals(realPassword)) || ( realCategory.equals("staff" ) ) ){
    UserSession.setAttribute("realUser", realUser);
    dispatcher = request.getRequestDispatcher("staffView.jsp");
    dispatcher.forward(request, response);
    }

    else if (( username.equals(realUser) ) || (password.equals(realPassword)) || ( realCategory.equals("teacher" ) ) ){
    UserSession.setAttribute("realUser", realUser);
    dispatcher = request.getRequestDispatcher("teacherView.jsp");
    dispatcher.forward(request, response);
    }

    else if ( ( realCategory.equals(" ") ) || ( realUser.equals("") ) || (realPassword.equals("")) ){
    response.sendRedirect("registerlogin.html");
    }
    }

    }
    catch(Exception e){
    System.out.println(e);
    }
    }
    }

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,137
    are u getting any errors? if so pls post those, if no errors then chances are there is nothing to write
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles