【问题标题】:Why the error occurs: 'error: package com.mysql does not exist'?为什么会出现错误:'error: package com.mysql does not exist'?
【发布时间】:2021-10-09 01:50:43
【问题描述】:

我正在尝试将 MySQL 数据库链接到我的 Jira 插件。我正在使用 Intellij IDEA 社区版,它说语法都很好。我的库中有 MySQL 连接器 .jar(通过 File->Project Structure 添加),所以它不应该是这样,除非在其他地方我必须添加它的 .jar 文件不会告诉我的。我刚刚通过从their website 下载添加了 MySQL .jar,但它不起作用。我添加了 maven MySQL .jar 并且没有用。我两个都加了,还是不行。 My external libraries

我在编译插件时收到此错误:

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

<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.Driver not found by PACKAGE"

如果您对填充 ArrayList 有任何疑问,它是用于动态填充 Jira 插件的 Select 列表。添加这些错误和测试后,我可以在我的 Jira 开发网站上看到它们。

【问题讨论】:

  • Class.forName() 行自 2006 年以来就不再需要了。删除它。除非您在某处有 import 声明,否则您不会收到此错误。也删除它。
  • @user207421 这就是问题所在。 Class.forName() 用于查看项目是否在库中看到该 mySQL 驱动程序。就像我在帖子中所说的那样,添加了`import`以在控制台中查看错误。如果没有这两个,我只知道 jdbc:mysql 没有连接,我不知道为什么。

标签: java mysql maven jdbc jira-plugin


【解决方案1】:

您为 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

【讨论】:

  • 这并没有解决问题。我不认为这是 maven pom.xml 的问题。我相信这是项目库的问题,即使我添加 .jar 文件的方式就像我发现的每个网站都说要为 Intellij 添加它。
  • 你为你的项目检查过这个吗? Intellij IDEA 不是健康指标,因为其他开发人员可能正在使用 Eclipse、NetBeans、Visual Studio。运行状况指示器是 Apache Maven 的命令行界面,因为它是您的构建工具。
  • 请为您的项目运行命令mvn clean test并附上结果。
  • @Dimitry Rakovets 我无法运行 mvn clean test,它说它不是批处理文件的一部分。我在 Intellij Maven 选项卡中运行了这个名为 clean 的东西,它说 Failure to find com.atlassian.maven.plugins:jira-maven-plugin:pom:8.1.2 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced 我还运行了 atlas-mvn clean test 并得到了这个 [INFO] No tests to run.
  • 执行atlas-mvn clean test命令后没有错误意味着您的代码正在编译。尝试使用命令atlas-mvn clean packageatlas-package 在.jar 中收集。
猜你喜欢
  • 2021-10-01
  • 2022-11-12
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 2023-01-12
  • 2023-02-23
  • 2015-09-14
  • 1970-01-01
相关资源
最近更新 更多