1

I use Eclipse juno. I have mysql-connector-java-5.1.22-bin.jar in my buildpath and i try to establish a DB connection to my mysql database. Here is my code

public Connection getConnectionToDB() {
    try {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url + dbName,
                userName, password);
        return con;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

When i debug it i found out the problem is at:

Class.forName(driver);

i also tried replacing it with:

Class.forName(driver).newInstance();

At this line, it breaks out to the exception and throws this exception:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at main.jdbc.BaseJdbc.getConnectionToDB(BaseJdbc.java:18)
at main.jdbc.UserDriverJdbc.queryUserById(UserDriverJdbc.java:15)
at main.drivers.UserDriver.findUserById(UserDriver.java:50)
at main.drivers.UserDriver.isLoginValid(UserDriver.java:56)
at main.controllers.LoginController.doPost(LoginController.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:164)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:164)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
juergen d
  • 201,996
  • 37
  • 293
  • 362
Murat Sayılgan
  • 41
  • 1
  • 1
  • 7

6 Answers6

3

The answer was quite tricky for me to find but i figure it out. It was ofcourse classpath problem.

If you ever encounter a problem like this and try to solve it inside eclipse, go to

eclipse -> window -> preferences -> java -> buildpath -> classpath

and add the

"mysql-connector-java-5.1.22-bin.jar"

as new variable. Name it whatever you like. Hope this helps.

Murat Sayılgan
  • 41
  • 1
  • 1
  • 7
0

You need to make sure you have the proper libraries and jar files loaded in your project. I dealt with this a lot when working with postgresql. I would think you're just missing the jar file.

Gene Parmesan
  • 102
  • 2
  • 12
  • as i stated i have I have mysql-connector-java-5.1.22-bin.jar in my buildpath :(. i also set my CLASSPATH variable with whatever other posts suggested. – Murat Sayılgan Oct 16 '12 at 17:38
  • have you looked at some of the other questions posted on site? I found this one which seems similar to what you are doing http://stackoverflow.com/questions/12601879/how-to-install-mysqlconnecter-java-correctly and this one http://stackoverflow.com/questions/12633149/java-lang-classnotfoundexception-com-mysql-jdbc-driver-when-run-jar-file. Otherwise I am at a loss of ideas. Your code looks correct. – Gene Parmesan Oct 16 '12 at 17:50
  • I just realise this problem could be eclipse juno related. I can't seem to make my log4j work too. I will dig it more. – Murat Sayılgan Oct 17 '12 at 10:20
0

This was my problem which was answered here: SO Link

"If this code works in your J2SE it means you need to have a JAR file somewhere containing com.mysql.jdbc.Driver class (so called JDBC driver). This JAR needs to be visible in Tomcat. So, I would suggest to place mysql-jdbc.jar at physical location to /WEB-INF/lib directory of your project." From

Community
  • 1
  • 1
beplaya
  • 281
  • 2
  • 8
0

I've been looking for a solution everywhere. Maybe this is not your solution, but using Eclipse i was exporting as a JAR file and got same exception, but then exported as "Runnable JAR"and it worked.

0

If you use netbeans then do the followings: go to your application>>>libraries>>add jar (by right click)>> "mysql-connector-java-5.1.22-bin.jar"

m.rahman
  • 1
  • 1
0

In Your Eclipse Go to Windows->Preferences->Java->BuildPath->ClassPathVariables->New

Now Add Your Mysql-Connector jar file what you downloaded From Mvn repository or anywhere ...Now ApplyAndClose ...restart the server it will work...

Note: If You are using Servlet then use eclipse from the package not from the installer..If you will use the installer you will get lot of problem