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

时间:2015-01-05 16:49:20

标签: java mysql maven jdbc

我正在尝试使用maven和java设计应用程序。它的一部分连接到MySQL数据库。为此,我使用Class.forName("com.mysql.jdbc.Driver");。我还添加了mysql-connector-java' dependency to my pom.xml. However when I run mvn test I receive the following error when the line connection = DriverManager.getConnection(“jdbc:mysql // kritsit.ddns.net:CaseTracker”,用户名,密码)`运行:

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>
...

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

1 个答案:

答案 0 :(得分:1)

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

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

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

(来自MySQL documentation的URI语法)