在测试阶段找不到 Maven 依赖项 (MySQL-Connector)

Maven dependency (MySQL-Connector) not found during testing phase

我正在尝试使用 Maven 和 java 设计一个应用程序。它的一部分连接到 MySQL 数据库。为此,我使用 Class.forName("com.mysql.jdbc.Driver");。我还添加了 mysql-connector-java' dependency to my pom.xml. However when I runmvn testI receive the following error when the lineconnection = DriverManager.getConnection("jdbc:mysql//kritsit.ddns.net:CaseTracker", username, password)` is run:

Running com.kritsit.casetracker.server.domain.services.DatabasePersistenceTest
java.sql.SQLException: No suitable driver found for jdbc:mysql//kritsit.ddns.net:CaseTracker
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.kritsit.casetracker.server.domain.services.DatabasePersistence.open(DatabasePersistence.java:26)
    at com.kritsit.casetracker.server.domain.services.DatabasePersistenceTest.testOpen(DatabasePersistenceTest.java:28)
    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:483)
    at junit.framework.TestCase.runTest(TestCase.java:154)
...

我的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.kritsit.casetracker</groupId>
  <artifactId>server</artifactId>
  <packaging>jar</packaging>
  <version>0.1a-SNAPSHOT</version>
  <name>CaseTracker Server</name>
  ...
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.34</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.1</version>
    </dependency>

    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>3.0.21</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
              <overWriteSnapshots>true</overWriteSnapshots>
              <overWriteRelease>true</overWriteRelease>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ....
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.kritsit.casetracker.server.CaseTrackerServer</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      ...
      <plugin>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
      </plugin>
      ....
   </plugins>
</build>
...

如何解决找不到合适的驱动程序的问题?

您的 JDBC URI 是否正确?它可能在 'mysql' 之后缺少一个冒号。尝试

jdbc:mysql://kritsit.ddns.net:CaseTracker

而不是jdbc:mysql//kritsit.ddns.net:CaseTracker

(来自 MySQL documentation 的 URI 语法)