在tomcat中找不到合适的驱动程序

时间:2018-04-15 18:44:28

标签: java mysql postgresql gradle jersey

我有一个非常奇怪的问题,我无法使用jersey连接到tomcat 8.5.30中的任何数据库,但我可以在没有tomcat的情况下运行它。

我的班级:

package me.mypackage;

import javax.ws.rs.*;
import java.sql.*;

@Path("somepath")
public class Main {

    @GET
    public void example() {
        connectToDatabase();
    }

    public static void connectToDatabase() {
        try {
            DriverManager.getConnection("jdbc:mysql://141.41.121.76:3306/database", "user", "pass");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String... args) {
        connectToDatabase();
    }
}

如果我运行main,它会成功连接到数据库,执行所有操作并运行更新,查询,一切顺利。

如果我运行tomcat并转到该URL,则会抛出此错误:

    java.sql.SQLException: No suitable driver found for jdbc:mysql://141.41.121.76:3306/database
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at me.lordcarlosmp.marketsurveyapi.Main.connectToDatabase(Main.java:16)
at me.lordcarlosmp.marketsurveyapi.Main.example(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.VoidVoidDispatcherProvider$VoidToVoidDispatcher.doDispatch(VoidVoidDispatcherProvider.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

使用此脚本构建gradle

plugins {
    id 'java'
    id 'war'
    id 'org.jetbrains.kotlin.jvm' version '1.2.31'
}

group 'me.mygroup'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

ext {
    jerseyVersion = '2.25.1'
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.31"
    compile "org.glassfish.jersey:jersey-bom:$jerseyVersion"
    compile "org.glassfish.jersey.core:jersey-server:$jerseyVersion"
    compile "org.glassfish.jersey.containers:jersey-container-servlet-core:$jerseyVersion"
    compile "org.slf4j:slf4j-api:1.7.25"
    compile "org.slf4j:slf4j-simple:1.7.25"
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'
    compile group: 'org.vibur', name: 'vibur-dbcp', version: '22.1'
    compile group: 'com.zaxxer', name: 'HikariCP', version: '2.7.8'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

mysql驱动程序位于/ libs / explosion / myproyect / WEB-INF / lib文件夹的类路径中,但项目中还有其他许多依赖项,并且都在运行时可用。

我试过了:

  • 几个mysql连接器版本(6.0.6,6.0.3,5.1.38,5.1.29 ...):同样的错误。

  • 几个MySQL数据库:同样的错误。

  • PostgreSQL数据库:同样的错误。

  • HickaryCP a Vibur:同样的错误。

  • Kotlin:同样的错误。

  • 不同的HTTP方法:同样的错误。

0 个答案:

没有答案