我想将从数据库中检索到的数据添加到一个数组中,但它的检索数据为空。我的错误是什么?请帮助我。
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/