1
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 java.lang.NullPointerException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Animesh Pandey
 */
public class anilet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn = null;
        String username = null;
        String userpass = null;
        String strQuery = null;
        Statement st = null;
        ResultSet rs = null;
        HttpSession session = request.getSession(true);
        try {
            out.println("I am here!1"); //gets printed
            [B][U][I]Class.forName("com.mysql.jdbc.Driver");[/I][/U][/B]
            conn =   DriverManager.getConnection("jdbc:mysql://localhost:3306/usermaster","root","");
            out.println("I am here!2"); // Does not ger printed
                if(request.getParameter("username") != null && request.getParameter("password") != null)
                    {
                        username = request.getParameter("username").toString();
                        userpass = request.getParameter("password").toString();
                        out.println(username);
                        out.println(userpass);
                       

                        strQuery = "select * from usermaster where username='" + username + "' and  password='" + userpass + "'";
                        System.out.println(strQuery);
                        st = conn.createStatement();
                        rs = st.executeQuery(strQuery);
                        int count=0;
                        while(rs.next()){
                            session.setAttribute("username",rs.getString(2));
                            count++;
                        }
                        out.println(count);
                        if(count>0){
                            response.sendRedirect("welcome.jsp");
                        }
                        else{
                            response.sendRedirect("login.jsp");
                        }
                    }
                else{
                    response.sendRedirect("login.jsp");
                }
                System.out.println("Connected to the database");
                conn.close();
                System.out.println("Disconnected from database");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

This servlet does not run after the "Class.forName("com.mysql.jdbc.Driver");", I mean to say that any code after this command does not run ... but it also does not give any error. The project also builds successfully. What could be the problem ????
:confused: :?:

Please help!

4
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by ghul-mur
0

Do you get an exception...?? Try finding out whether conn is null. This will confirm whether connection is being established or not in the first place.

0

Do you get an exception...?? Try finding out whether conn is null. This will confirm whether connection is being established or not in the first place.

...
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/usermaster","root","");
out.println(conn);
...

'conn' is not being printed .... !
Any code after Class.forName() is not working ... and I am also not getting any error or exception!

0
...
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/usermaster","root","");
out.println(conn);
...

'conn' is not being printed .... !
Any code after Class.forName() is not working ... and I am also not getting any error or exception!

I have added a few more things but I still I cannot get the reason for the error!

try {
            out.println("Animesh");
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/usermaster","root","");
            if(!conn.isClosed())
                System.out.println("Successfully connected to " + "MySQL server using TCP/IP...");
.
.
.

This page still prints only Animesh .... and nothing after that !!!!

0

Are you running your code in an ide or via command line. Also , check the condition if(conn == null). If it is null it means connection is not being established.

0

You also need to post the exception that you get. Did you put in the classpath the my-sql-connector.jar ?

0

You also need to post the exception that you get. Did you put in the classpath the my-sql-connector.jar ?

Thank you all ...
My problem has been solved! :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.