gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:29:29 31 4
gpt4 key购买 nike

我正在尝试使用 maven 和 java 设计一个应用程序。它的一部分连接到 MySQL 数据库。为此,我使用 Class.forName("com.mysql.jdbc.Driver"); 。我还添加了 mysql-connector-java' dependency to my pom.xml. However when I run mvn测试I receive the following error when the line连接 = 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 如下(为了简洁,省略号显示了省略的 block :

<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 语法)

关于java - 在测试阶段未找到 Maven 依赖项(MySQL-Connector),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27784093/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com