gpt4 book ai didi

java - 将数据导出到双数组

转载 作者:搜寻专家 更新时间:2023-10-30 20:26:24 25 4
gpt4 key购买 nike

我想将从数据库中检索到的数据添加到一个数组中,但它的检索数据为空。我的错误是什么?请帮助我。

for (User u : userLists) {
for (Resource r : resourceLists) {

String ratingQuery
= "SELECT rank FROM ratings WHERE userindex='"
+ u.getIndex()
+ "' AND resourceindex='"
+ r.getIndex()
+ "'";

try {
ratingCon = ConnectionManager.getConnection();
stmt = ratingCon.prepareStatement(ratingQuery);
rs = stmt.executeQuery();

while (rs.next()) {
rate = rs.getDouble("rank");
ratings[u.getIndex()][r.getIndex()] = rate; //it seems here is the problem
}

} catch (Exception e) {
System.out.println("Log In failed: An Exception has occurred! " + e);
}
}
}

最佳答案

这一行没有问题,如果ratings是double的二维数组:

ratings[u.getIndex()][r.getIndex()] = rate;

但是您的数据库可能返回空的 ResultSet 吗?插入打印语句进行调试:

  while (rs.next()) {
rate = rs.getDouble("rank");
System.out.println("Result set is not empty! Current rank is: " + rate);
ratings[u.getIndex()][r.getIndex()] = rate; //it seems here is the problem
}

如果它什么都不打印——那么问题不在你的代码中,而是在数据库数据中。另外,我认为您在查询末尾错过了分号 ;

关于异常处理:

 catch (Exception e) {
System.out.println("Log In failed: An Exception has occurred! " + e);
}

您在此处捕获的异常类型过于抽象 - 您无法知道它是 SQLException 异常还是 ArrayIndexOutOfBoundsException。将其更改为多个 catch block 。

关于java - 将数据导出到双数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31886506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com