-1

I'm working on my first JDBC web app and trying to get it to connect to my MySQL database.

The SQL service is running as far as I can tell since it's visible from the the processes tab in Task Manager. I've also added the jar to my build path and its clearly visible under Java Resources>src>Libraries>mysql-connector.jar.

However, I still get this error :

Error loading driver: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

This is the only code left in my doGet method:

try {
    Class.forName("com.mysql.jdbc.Driver");
} 
catch (ClassNotFoundException cnfe) 
{
    System.out.println("Error  loading  driver:  " + cnfe);
}
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
  • But does your deployed application also have the MySQL connector JAR? By the way, I believe it is no longer necessary to use `Class.forName` to explicitly load the JDBC JAR. – Tim Biegeleisen Sep 20 '16 at 01:39
  • So many dupe targets for this. Justin search better. – Drew Sep 20 '16 at 01:40
  • @Drew - nice gold badge in [tag:mysql], that was a grind for sure! you are dedicated my friend! –  Sep 20 '16 at 01:48
  • 1
    @JarrodRoberson thx. A slow grind. Setting up servers then answering their question :p – Drew Sep 20 '16 at 01:50
  • @Drew did you see `Java Resources>src>Libraries>mysql-connector.jar`? Using this path means Justin use an IDE(I think it's eclipse).So it's a question about how to add jar to classpath in the used IDE. :) – fairjm Sep 20 '16 at 07:42
  • Then http://stackoverflow.com/q/17484764 – Drew Sep 20 '16 at 08:07
  • I searched for the better part of an hour and didn't find the solution, but I may have just missed it. What ended up working for me was moving the jar file to the WEB-INF lib folder in eclipse. – Justin Walters Sep 21 '16 at 01:54

1 Answers1

-1

Make sure your jar is under classpath.
If you use eclipse, you can right click the jar and choose build Path > add to build path.
Maybe you should try using maven or other dependency managers.It's easier to use them than managing jar yourself.

fairjm
  • 1,115
  • 12
  • 26