我在本地完成了一个java动态Web应用程序的构建,现在我正在尝试将它部署在Digital Ocean上的Droplet上。
我在服务器上安装了jdk,mysql和tomcat,并使用从eclipse导出的.war文件部署了我的应用程序。我还将mysql-connector-java文件复制到/ opt / tomcat / lib文件夹。
但是当我在服务器上打开我的应用程序时,会出现如下错误:
Error instantiating servlet class surveyExchange.my.SignUpServlet
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/UserInfo?useSSL=false.
我的代码看起来像这样:
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);
}
这一行出错:
conn = DriverManager.getConnection(url + dbName, userName, password);
堆栈追踪:
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)
非常感谢您的耐心等待!
答案 0 :(得分:0)
回答你问题的间接方式:Go with a datasource(但你可能会遇到同样的问题,所以继续)
驱动程序在tomcat的lib/
文件夹中应该没问题(不在WEB-INF中,但无论你做什么,请只在一个位置使用它)。
检查文件的权限(以及所有目录,关于您用来运行tomcat的用户),并确保您的连接器实际上是一个jar文件而不是仅包含jar的压缩下载文件。
我通常做的是aptitude install libmysql-java
以确保我的版本与本地mysql匹配,然后ln -s /usr/share/java/mysql-connector-java.jar /opt/tomcat/lib/
尝试连接之前尝试的另一个选项(每个应用程序运行一次就足够了):执行Class.forName("com.mysql.jdbc.Driver");
。
另外,我认为最好将连接分配转移到servlet的init()
方法(并在destroy()
中清理),甚至使用JNDI连接 - 但这只是除了上面的答案。