简体   繁体   中英

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

I am trying to design an application using maven and java. Part of it connected to a MySQL database. To do this I am using Class.forName("com.mysql.jdbc.Driver"); . I have also added the mysql-connector-java' dependency to my pom.xml. However when I run 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", 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)
...

My maven pom.xml is as follows (ellipses show left out blocks for brevity:

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

How do I solve this no suitable driver found issue?

Is your JDBC URI correct? It might be missing a colon after 'mysql'. Try

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

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

(URI syntax from the MySQL documentation )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM