0

I'm working on a web application for a class of mine. The assignment is to create a College Services app where students can login and register for classes. Right now I'm working on the login portion and it coded out but I'm having some issue and hopeing I get a little help.

After running the debugger in netbeans, I've noticed that is breaking at the

connection = DriverManager.getConnection(dburl, login, pass);

of the ConnectionManager.java.

I was wondering if someone could take a look at my code and see if they can see anything I'm doing wrong.

LoginServlet.java

package login;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import business.*;
import data.*;

public class LoginServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");

        HttpSession session = request.getSession();

        User user = new User();
        user.setUserId(userId);
        user.setUserPass(password);
        user = LoginValidate.validate(user);

        if(user.isSuccessful())
        {
            session.setAttribute("user", user);
            String url = "/studenthome.jsp";
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }
        else if(!user.isSuccessful())
        {
            response.sendRedirect("index.jsp");
        }


    }
    /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        this.doGet(request, response);
    }*/
}

LoginValidate.java

package data;

import java.sql.*;

import business.*;

public class LoginValidate
{
    public static User validate(User user)
    {
        String u = user.getUserId();
        String p = user.getUserPass();
        String query = "select * from students where StudentId='" + u + "' AND StudentPass='" + p +"'";

        try
        {
            Connection connection = ConnectionManager.getConnection();

            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            String rsUser = resultSet.getString(0);
            String rsPass = resultSet.getString(1);
            //boolean userExist = resultSet.next();
            if(u.equals(rsUser) && p.equals(rsPass))
            {

                String fName = resultSet.getString("FirstName");
                String lName = resultSet.getString("LastName");
                user.setUserFName(fName);
                user.setUserLName(lName);
                user.setSuccessful(true);
            }
            else
            {
                user.setSuccessful(false);
            }

        }
        catch(Exception e)
        {

        }
        return user;
    }
}

ConnectionManager.java

package data;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager
{

    static Connection connection;

    public static Connection getConnection()
    {
            try
            {
                String dburl = "jdbc:mysql://localhost:3306/collegeservices";
                String login = "root";
                String pass = "root";
                connection = DriverManager.getConnection(dburl, login, pass);

            }//end try 
            catch(SQLException sqle)
            {
                for(Throwable t : sqle){
                    t.printStackTrace();
                }
            }//end catch

        return connection;
    }//end getConnection()
}

I've been stuck on this and can't seem to see what's wrong. WHen it gets to taht connection = DriverManager.getConnection(dburl, login, pass) it drops down to the catch block.

Any insight to fixing this problem is greatly appreciated

thanks

jay

2
Contributors
4
Replies
10
Views
4 Years
Discussion Span
Last Post by jmw5598
0

hai jmw5598,

i think you have not loaded the mysql related driver in order to provide communication between your application and databse

for this , add this line before at line 20 in your ConnectionManager class

Class.forName("com.mysql.jdbc.Driver");

then everything goes fine

please refer this url it explains everything

http://www.vogella.com/articles/MySQLJava/article.html

check it once and let me know

Edited by radhakrishna.p: giving clarity on the solution

0

I left out loading the DB driver since my professor said it's not need. He said it's automatically loaded now. But I added it anyways and now as I step through my application in debug mode, and when It gets to

    Class.forName("com.mysql.jdbc.Driver");

it drops down to my catch block and still executes the catch block when I try to getConnection as well.

Is it possible that I don't have the Driver file installed in Netbeans properly? I'm able to access my database under Services and making a connection through there with the Connector J.

Do I have to have a copy of the driver placed directly in my Application?

Sorry if these are ignorant questions, but I'm learning. This is my modefied code with the added driver load code.

package data;


import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager
{

    static Connection connection;

    public static Connection getConnection()
    {

            try
            {
                String dburl = "jdbc:mysql://localhost:3306/collegeservices";
                String login = "root";
                String pass = "root";
                try
                {
                    Class.forName("com.mysql.jdbc.Driver");
                }
                catch(ClassNotFoundException cnfe)
                {
                    cnfe.printStackTrace();
                }
                connection = DriverManager.getConnection(dburl, login, pass);

            }//end try 
            catch(SQLException sqle)
            {
                for(Throwable t : sqle){
                    t.printStackTrace();
                }
            }//end catch

        return connection;
    }//end getConnection()
}
0

hai jmw5598,

i am not much aware of Connector J but
i forgot to tell one more thing

you need to add/copy/include the mysql related jar file to your project libraries that jar file you can find in your database installation folder

i mean YOUR_DB_instalation_folder/lib/ (please check there you can find it there)

please try to do like that

and let me know the status

haooy coding

0

hey radhakrishna.p,

I added the conector/j jar to my library folder in my project, retested it and everything is working now. I appriciate all your help.

thanks again

jay

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.