954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem in servlet-mysql connection!

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
            <strong><em>Class.forName("com.mysql.jdbc.Driver");</em></strong>
            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!

apanimesh061
Junior Poster
176 posts since Nov 2010
Reputation Points: 10
Solved Threads: 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.

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
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!

apanimesh061
Junior Poster
176 posts since Nov 2010
Reputation Points: 10
Solved Threads: 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 !!!!

apanimesh061
Junior Poster
176 posts since Nov 2010
Reputation Points: 10
Solved Threads: 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.

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
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

apanimesh061
Junior Poster
176 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I have same problem how did you solved it. Please let me know!

ghul-mur
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You