简体   繁体   中英

No suitable driver found for jdbc://localhost

I have a simple servlet deployed on a Jetty 9 server on Ubuntu 14.04. It was working fine till yesterday. But after a system crash, i had problems with MySQL server, so had to make configuration changes to the mysql server to make it work ie added the line socket = /tmp/mysql.sock under sections [mysqld] and [client] in my.cnf

But now the servlet is not working. It gives me the below error.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/slappdb
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at slappserver.RegisterServlet.doGet(RegisterServlet.java:74)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:738)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:551)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:478)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:462)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:279)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:534)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Unknown Source)

I have made no changes to the servlet code at all :

final String DB_URL = DatabaseParameters.SlappDbURL;
final String USER = DatabaseParameters.DbServer_Username;
final String PASSWORD = DatabaseParameters.DbServer_Password;
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);

The driver is at the same place as before : /home/faizal/DEV/jre1.7.0_55/lib/ext/mysql-connector-java-5.0.8-bin.jar , which is included in the build path of the project.

What could have gone wrong?

Something seemed to have got corrupted in my project because of the crash. It is working now after i added the line Class.forName("com.mysql.jdbc.Driver"); in the servlet. But this line is actually not required since i am using a JDBC 4 driver.

Interestingly, now even if i remove this line, it works. So, i am still not sure what was the problem, but atleast it's working as before without any change in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM