为什么会出现错误:"错误:包 com.mysql 不存在"?



我试图将MySQL数据库链接到我的Jira插件。我正在使用Intellij IDEA社区版,它说语法都很好。我在我的库中有MySQL连接器.jar(通过File->Project Structure添加),所以它不应该是这样,除非有其他地方我必须添加.jar文件,它不会告诉我。我只是通过从他们的网站下载MySQL .jar添加,它不会工作。我添加了maven MySQL .jar,但这不起作用。我把它们都加了,还是不行。我的外部库

当我编译插件时,我得到这个错误:

Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
/C:/Users/USER/dynamicSelectCF/src/main/java/PACKAGE/DatabaseConnection.java:[11,1] package com.mysql does not exist

使用下面的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.mysql.*;

public class DatabaseConnection {

public static ArrayList<String> connection(ArrayList<String> population){
population.clear();
population.add("-------------------");
Connection connection = null;
Statement statement = null;
ResultSet result = null;
String url = Constants.getUrl();
String username = Constants.getUsername();
String password = Constants.getPassword();
String query = "SELECT name, p_o_allowed FROM database WHERE p_o_allowed LIKE 'P%'";
try{
Class.forName("com.mysql.cj.jdbc.Driver");
} catch(ClassNotFoundException e){
population.add("mysql driver a no no");
population.add(e.getMessage());
population.add(e.toString());
e.printStackTrace();
}
population.add("-------------------");
try{
population.add("trying connection");
connection = DriverManager.getConnection(url, username, password);
population.add("trying statement");
statement = connection.createStatement();
population.add("trying result");
result = statement.executeQuery(query);
population.add("all 3 are a go!");
while(result.next()){
population.add(result.getString("name"));
}
}catch (SQLException e){
population.add("Yeah we didn't get in fam");
population.add(e.getMessage());
e.printStackTrace();
}finally{
closeResultSet(result);
closeStatement(statement);
closeConnection(connection);
}
return population;
}

我也有这个在我的pom.xml文件,这是直接从MySQL网站。

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
<scope>provided</scope>
</dependency>

我知道我不需要import com.mysql;然而,这是在编译到我的终端时生成错误的唯一方法,否则它会转到try/catch并抛出"com.mysql.cj.jdbc。PACKAGE">.

未找到驱动程序。如果你对填充数组列表有任何疑问,它是为Jira插件动态填充一个选择列表。添加这些错误和测试使我能够在Jira开发网站上看到它们。

您为mysql-connector-java指定了错误的依赖范围。下面的依赖项解决了这个问题:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
<scope>runtime</scope>
</dependency>

更多信息:Apache Maven: Dependency Scope

相关内容

最新更新