我正在努力尝试迭代结果集并能够将我检索到的记录存储到我的类Threat.java中的对象的ArrayList中。我是一个相当初学者的程序员,因为不幸的是,我没有获得一致的学习经验,所以我对于如何使这个过程工作缺乏了。 Database.java处理所有数据库程序(连接,获取结果集,分配字段),MainDisplay.java是我想完成大部分工作的地方,至少现在是这样。 Threat.java有多个变量,但我只关心能够成功打印server_id。
我得到的错误是在MainDisplay中我有add()方法的行,其中显示:“ArrayList类型中的方法add(威胁)不适用于参数(void)”
任何人都可以帮助解释我的情况吗?我想我的组织混乱不堪,并且很难理解如何通过方法而不是通过参数来准确添加字段。
更新:我对公共字符串setServer_Id(字符串服务器)进行了更改,但仍然有相同的错误
Database.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;
}
Threat.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();
}
}
}
答案 0 :(得分:0)
此方法:
threat.setServer_Id(serverHolder)
返回void,没有。因此,在ArrayList中添加“void”是没有意义的。您需要仔细考虑要添加到列表中的内容,然后相应地更改代码。
您需要为Threat类提供合适的构造函数,并使用数据库中的信息创建 new 威胁对象,并将 添加到ArrayList。< / p>
答案 1 :(得分:0)
问题在于:
StatusInfo.add(threat.setServer_Id(serverHolder));
您需要将Threat
个对象添加到ArrayList,但上面的代码行正在尝试添加从threat.setServer_Id(serverHolder)
返回的void