Maven 缺少 MySQL 依赖项

我在 Eclipse 中有一个 Maven 项目(使用 ubuntu),其中有一个类 dbtest 可以访问我在 MySQL 中的 SQL 表,其中我使用的是 Apache Maven 3.3.9 和 Java 1.8.0_181。


当我使用 Eclipse 运行和编译代码时,它运行良好(我有一个名为 acm 的数据库,使用“root”作为我的用户名和密码)但是当我使用 Maven 命令创建一个 Jar 时:


mvn 全新安装


我编译它我收到以下错误:


java -cp target/maven-1.jar test.dbtest

start

Class-start

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/acm

End main

我关注了https://stackoverflow.com/questions/1074869/find-oracle-jdbc-并尝试了许多选项,例如 Peter Enis 的回答或将编译范围添加到依赖项,但没有一个解决了问题。而且,我也下载了mysql-connector-java jar(我试过很多版本),添加到库路径下,也没有用,所以我把解压出来的jar添加到路径中,问题依旧。

评论,我很怀疑我是否需要这一行,Class.forName("com.mysql.jdbc.Driver");//.newInstance();我把它留作评论(当我使用它时,我只得到那个 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver)因为有人说我应该和其他人说不会改变。


隔江千里
浏览 546回答 2
2回答

汪汪一只猫

Class.forName("com.mysql.jdbc.Driver").newInstance();需要注册驱动程序,如6.1 使用 JDBC DriverManager 接口连接到 MySQL 中所述。该错误java.lang.ClassNotFoundException: com.mysql.jdbc.Driver意味着包含com.mysql.jdbc.Driver类的 JAR 文件不在类路径中。最简单的修复方法是更改-cp值:java -cp path/to/mysql-connector-java.jar:target/maven-1.jar test.dbtest还有其他的方法来做到这一点如使用MANIFEST.MF具有java -jar或建立使用超级罐子的Maven插件阴影。

DIEA

在你的 pom 中添加这个插件并执行java -jar your_project-jar-with-dependencies.jar&nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-assembly-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>single</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.dbtest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRef>jar-with-dependencies</descriptorRef>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; </plugin>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java