【发布时间】: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