java - 将数据导出到双数组

标签 java arrays database

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

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/

相关文章:

java - 我想要一个接受数组、低数字、高数字的方法。并返回一个数组,其中仅包含数字

java - Eclipse 中的奇怪标志

Javascript reduce() 查找字符串中最短的单词

python - 用于有条件减去现有值的 Numpy boolean 索引掩码

java - Java 中的 Arduino Map 等效函数

java - 执行 JPQL 查询的工具?

javascript - 我的 ng-repeat 只显示最新的循环

java - 在 preparedStatement 中编辑查询

c# - 在运行时更改 DbContext 连接

MySQL查询: how to count grouped column but return ids(comma separated) in the current group