SQLException:没有为两个maven项目之间的jdbc:mysql找到合适的驱动程序

Rob*_*ani 9 mysql eclipse jdbc sqlexception maven

我在Eclipse(v4.7.0)工作区中有2个maven项目.

第一个项目包含一些实用程序内容,并通过JDBC驱动程序保存与MySQL数据库的连接.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>dbtools</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>DBTools</name>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <!-- JDBC for MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

第一个项目是作为jar构建的,它包含在第二个项目(包含主应用程序)中作为maven依赖项,如下面的pom.xml所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.example</groupId>
 <artifactId>mainapp</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>MainApp</name>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <jersey2.version>2.25.1</jersey2.version>
    <jaxrs.version>2.0.1</jaxrs.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.6.1</version>
            <configuration>
                <filesets>
                    <fileset>
                        <directory>C:/apps/tomcat/webapps/mainapp</directory>
                        <includes>
                            <include>**</include>
                        </includes>
                        <followSymlinks>false</followSymlinks>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-appCtx</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>C:/apps/tomcat/webapps/</outputDirectory>
                        <overwrite>true</overwrite>
                        <resources>
                            <resource>
                                <directory>../mainapp/target</directory>
                                <includes>
                                    <include>mainapp-0.0.1-SNAPSHOT.war</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- JAX-RS -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>${jaxrs.version}</version>
    </dependency>
    <!-- Jersey 2.25.1 -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey2.version}</version>
    </dependency>

    <!-- Local DBTool -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>dbtools</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

作为主应用程序的第二个项目被部署为war文件.当我启动Tomcat(第二个应用程序的战争)时,我在运行时得到了一个SQLException:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false
at java.sql.DriverManager.getConnection(DriverManager.java:689)
Run Code Online (Sandbox Code Playgroud)

我在StackOverflow中已经阅读了几个关于此异常的问题,但我仍然没有找到一个有效的解决方案:(

在我的Tomcat安装文件夹的lib文件夹中,我放置了mysql-connector-java-6.0.6.jar.

我还注意到在第一个项目的JAR文件中(将其作为存档打开),里面没有JDBC连接器.这是正常的吗?

在第一个项目中,我以这种方式建立连接:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/torre?autoReconnect=true&useSSL=false", "dbuser", "dbpass");
Run Code Online (Sandbox Code Playgroud)

conn的类型为java.sql.Connection.

我也尝试过:

Class.forName("com.mysql.jdbc.Driver");
Run Code Online (Sandbox Code Playgroud)

在che"conn = ..."之前,但我得到了相同的结果:(

我正在使用Tomcat 8.5和JDK 1.8.

我有什么想法可以摆脱这个问题?我在Maven或Eclipse构建配置中遗漏了什么吗?

在此先感谢您的帮助!:)

Amp*_*esh 1

根据我的理解提供输入

你提到过

我还注意到,在第一个项目的 JAR 文件(将其作为存档打开)中,内部没有 JDBC 连接器。正常吗?

当你构建 jar 时,与 war 不同,你将找不到依赖项[只有一个没有 maven-shade-plugin 的 jar,它不会有依赖项]

所以使用maven-shaded-plugin https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

<project>        
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>com.yourpackage.YourClass</Main-Class>
                    <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
                    <X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
Run Code Online (Sandbox Code Playgroud)

在你的 pom.xml [project-1 中用于生成 jar]

现在,如果您看到在 maven 构建后您将有两个 jar(一个是原始的,另一个 jar 具有 pom 中的依赖项)

如果您提取具有更大大小的[具有依赖项的],您可以在此处找到 jdbc jar

现在项目2

当您将project-1作为依赖项包含时[确保包含较大的一个而不是较小的一个(没有依赖项)]如果您在推送时使用远程存储库,则较大的一个将被推送,因此当您提及它时在 pom [project-1] 中,构建后将下载一次更大的 jar 检查 project-1 jar 是否有自己的依赖项

最后构建战争并部署

资料来源:做过同类场景,看起来和我之前做过的很相似

让我知道结果 [大多数情况下它会起作用,如果不起作用的话 mycontact :es12b1005@iith.ac.in 将尽力提供帮助]

谢谢 :)