我正在努力尝试遍历结果集并能够将我检索到的记录存储到来自我的类 Threat.java 的对象的 ArrayList 中。我是一个相当初学的程序员,被扔进了一个大型的 Java 项目,所以不幸的是,我没有一致的学习经验,所以我对如何使这个过程工作缺乏了解。Database.java 处理所有数据库过程(连接、获取结果集、分配字段),而 MainDisplay.java 是我想要完成大部分工作的地方,至少现在是这样。Threat.java 有多个变量,但我现在只关心能否成功打印 server_id。
我得到的错误出现在我在 MainDisplay 中有 add() 方法的那一行,上面写着:“ArrayList 类型中的方法 add(Threat) 不适用于参数 (void)”
任何人都可以帮助阐明我的情况吗?我认为我的组织很混乱,并且很难理解如何准确地通过方法而不是通过参数添加字段。
更新:我对 public String setServer_Id(String server) 进行了更改,但仍然有相同的错误
数据库.java
public class Database {
String details = null;
ResultSet rslt = null;
private int threat_level;
private String target;
private String server_id;
private int client_id;
private String attacker;
private String num_this_attack;
Connection con;
public void createConnection() {
//sensitive info
}
public ResultSet getData() {
String query = "SELECT target_stats.server_id, target_stats.target, target_stats.threat_level, target_stats.client_id, attack_stats.attacker, attack_stats.num_this_attack " +
" FROM target_stats " +
" LEFT OUTER JOIN attack_stats " +
" ON target_stats.target = attack_stats.target " +
" WHERE target_stats.num_attacks > '0' " +
" AND target_stats.interval_id>'2'";
Statement stmt = null;
try {
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
try {
rslt = stmt.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
return rslt;
}
public void process() {
try {
server_id = rslt.getString("server_id");
target = rslt.getString("target");
threat_level = rslt.getInt("threat_level");
client_id = rslt.getInt("client_id");
attacker = rslt.getString("attacker");
num_this_attack = rslt.getString("num_this_attack");
//details = "Target IP: " + target + " Server ID: " + server_id + " Client ID: " + client_id + " Threat Level: " + threat_level + " Attacker IP: " + attacker + " Number of attacks: " + num_this_attack;
//System.out.println(details);
} catch (SQLException e) {
e.printStackTrace();
}
public String getServer_id(){
return server_id;
}
威胁.java
public class Threat {
private String server_id;
private String target;
private int threat_level;
private String client_id;
//ArrayList<ArrayList<String>> attackerXnumberY = new ArrayList<ArrayList<String>>();
public Threat(){
server_id = null;
}
public String setServer_Id(String server){
server_id = server;
return server_id;
}
public String getServer_Id(){
return server_id;
}
}
MainDisplay.java
public class MainDisplay {
private static String serverHolder;
public static void main(String[] args) {
Database Data = new Database();
//Threat threat = new Threat();
ArrayList<Threat> StatusInfo = new ArrayList<Threat>();
Data.createConnection(); //Create connection to database
Data.getData(); //Runs query and returns result set
try {
while(Data.rslt.next()){
Data.process();
serverHolder = Data.getServer_id();
//StatusInfo.add(threat.setServer_Id(serverHolder));
Threat threat = new Threat();
StatusInfo.add(threat.setServer_Id(serverHolder));
System.out.print(StatusInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}