1

I am trying to run a dynamic web project and have established database connection as well. However I am getting "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" exception again and again when I am trying to run the following piece of code:

    public class ConnectionC
    {
      public static Connection connectDao()
      {
    try
    {

        Connection dbconn= null;
        String url = "jdbc:mysql://localhost:3306/practsql";
        String user="root";
        String password="tiger";

        Class.forName("com.mysql.jdbc.Driver");
        dbconn=DriverManager.getConnection(url, user, password);
        return dbconn;
    }
    catch(ClassNotFoundException e)
    {
        System.out.println(e);
    }
    catch(SQLException e)
    {
        System.out.println(e);
    }
    return null;    


}

public static void commitChange()
{
    try
    {
        Connection dbconn= null;
        String url = "jdbc:mysql://localhost:3306/practsql";
        String user="root";
        String password="tiger";

        Class.forName("com.mysql.jdbc.Driver");
        dbconn=DriverManager.getConnection(url, user, password);
        dbconn.commit();
    }
    catch(ClassNotFoundException e)
    {
        System.out.println(e);
    }
    catch(SQLException e)
    {
        System.out.println(e);
    }



}
}

I added jar file "mysql-connector-java-5.1.27-bin" also in the build path. However its showing the same error.

user3011373
  • 17
  • 1
  • 5

1 Answers1

1

You need to add it to run time path (classpath). Most of the times build path is limited to only compile time, not runtime.

If your application is web app, adding jar to your web app --> lib folder should resolve this issue.

kosa
  • 65,990
  • 13
  • 130
  • 167