2

I finished building a java dynamic web application locally, and now I am trying to deploy it on a droplet on Digital Ocean.

I have installed jdk, mysql, and tomcat on my server, and I deployed my app using the .war file exported from eclipse. I also copied a mysql-connector-java file to the /opt/tomcat/lib folder.

But when I open my app on the server, an error appeared like this:

Error instantiating servlet class surveyExchange.my.SignUpServlet
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/UserInfo?useSSL=false.

And my code looks like this:

static String url = "jdbc:mysql://localhost:3306/";
static String dbName = "UserInfo?useSSL=false";
static String driver = "com.mysql.jdbc.Driver";
static String userName = "root";
static String password = "******";
static Connection conn = null;

private static final long serialVersionUID = 1L;

/**
 * @throws SQLException 
 * @see HttpServlet#HttpServlet()
 */
public SignUpServlet() throws SQLException {
    super();
    conn = DriverManager.getConnection(url + dbName, userName, password);
}

There was an error on this line:

conn = DriverManager.getConnection(url + dbName, userName, password);

Stack Trace:

Exception
javax.servlet.ServletException: Error instantiating servlet class surveyExchange.my.SignUpServlet
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

Root Cause:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/UserInfo?useSSL=false
    java.sql.DriverManager.getConnection(DriverManager.java:596)
    java.sql.DriverManager.getConnection(DriverManager.java:215)
    surveyExchange.my.SignUpServlet.<init>(SignUpServlet.java:34)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    java.lang.Class.newInstance(Class.java:383)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

Thank you very much for your patience!!

desperado
  • 213
  • 1
  • 11
  • It looks like this: Error instantiating servlet class surveyExchange.my.SignUpServlet java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/UserInfo?useSSL=false. – desperado Apr 03 '16 at 03:24
  • Take one more variable driver=URL+dbName; and pass this argument .getConnection(driver, username, password) – Piyush Gupta Apr 03 '16 at 03:59
  • 1
    Welcome to Stack Overflow! I'm not sure what the answer is to your question, but you've done quite a nice job to make sure all the necessary information is included and well formatted. I'm sure someone will answer soon. Enjoy your stay! – Matt C Apr 03 '16 at 04:01
  • Thank you, everyone! I have updated my stack trace! – desperado Apr 03 '16 at 04:46
  • I've put the mysql-connector jar file in web-inf/lib folder but the error message and the stack trace are still the same... – desperado Apr 03 '16 at 05:09

1 Answers1

0

The indirect way of answering your question: Go with a datasource (but you might run into the same issues there, so continue)

The driver should be fine in tomcat's lib/ folder (not in WEB-INF, but whatever you do, please only have it in one location).

Check the permissions on the file (and all directories, with regards to the user you're using to run tomcat), and make sure your connector is actually a jar file and not a zipped downloaded file that only contains the jar.

What I typically do is to aptitude install libmysql-java to make sure I have the version matching the local mysql, then ln -s /usr/share/java/mysql-connector-java.jar /opt/tomcat/lib/

Another option to try before you attempt a connection (once per application run is enough): execute Class.forName("com.mysql.jdbc.Driver");.

Also, I'd argue that it's better for the connection allocation to be moved to a servlet's init() method (and clean up in destroy()) or even to use a JNDI connection - but that's only in addition to the answer above.

Community
  • 1
  • 1
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90