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)
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.
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.
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
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.
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"
来源:https://stackoverflow.com/questions/12920265/com-mysql-jdbc-driver-not-found-with-mysql-connector-in-buildpath