java - 结果集未正确定位

标签 java postgresql resultset

我有关于下一个电话的问题!

我写了一段代码,它必须计算一个关键字在我的 ArrayList 中出现了多少次;但是当循环为偶数时;它不起作用!

我知道这里有很多已解决的;但我无法解决我的代码中的错误。 这是我的代码:

    public static ResultSet getBins(Connection conn, String tableName){
    ResultSet distinctBin = null;
    try {
        Statement distinctBinsSt = conn.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
        distinctBin = distinctBinsSt.executeQuery("select bin, placedescr from " + tableName + " ORDER BY bin ASC limit 151");

    }catch(SQLException e){e.printStackTrace();}
    return distinctBin;
}
public static Map <String,Integer> placeDescCounter(ArrayList<String> binDescription ){
    Map<String, Integer> wordCount = new HashMap<String, Integer>();
        FlickrReader tagfileReader = new FlickrReader();
        for (String placeTag: binDescription)
    {
        Integer count = wordCount.get(placeTag);
        wordCount.put(placeTag, (count==null) ? 1 : count+1);
    }
    return wordCount;

}
public static ResultSet getBinInfo(Connection conn, String tableName){
    ResultSet binInfo =  null;
    try {
        Statement binInfoSt = conn.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        binInfo = binInfoSt.executeQuery("select bin, count(bin) from " + tableName + " group by bin order by bin ASC;");
        binInfo.last();
        binInfo.beforeFirst();
    }catch(SQLException e){e.printStackTrace();}
    return binInfo;
}
public static void main(String[] args) throws SQLException, ClassNotFoundException{
Connection conn = connectPG();
    String tableName= "tagcounterdebug";
    ResultSet bins = getBins(conn, tableName);
    ResultSet binsInfo = getBinInfo(conn,tableName);
    String placeDescriptions;
    List<String> placeDescription;
    ArrayList<String> binDescriptions = new ArrayList<String>();
    int bin =0;
    bins.last();bins.beforeFirst();

    while(binsInfo.next()){
        int defaultBin = binsInfo.getInt("bin");
        int binSize = binsInfo.getInt("count");
        printDebug("loop conter " + (binSize - 1));
        bins.next();
        for (int i=0; i < binSize; i++){
            bin = bins.getInt("bin");
            placeDescriptions = bins.getString("placedescr").toLowerCase();
            placeDescriptions = placeDescriptions.substring(placeDescriptions.indexOf("[")+1 , placeDescriptions.indexOf("]"));
            placeDescription = Arrays.asList(placeDescriptions.split("\\s*;\\s*"));
            HashSet<String> hashSetDescriptions = new HashSet<String>();//create a HashSet for tokens to calculate less
            hashSetDescriptions.addAll(placeDescription);
            binDescriptions.addAll(hashSetDescriptions);
            bins.next();
        }
        printDebug("****\n defBin: " +defaultBin + "   bin:  " +bin + "\n****");
        Map<String, Integer> tagCount = new HashMap<String, Integer>();
        tagCount = placeDescCounter(binDescriptions);
        for (String name: tagCount.keySet()){
            String key =name.toString();
            String value = tagCount.get(name).toString();
            System.out.println(key + " " + value);
        }
    }
}
}

最佳答案

您可能会发现这对您有帮助:https://stackoverflow.com/help/mcve

您正在调用 .next() 两次,内部调用会将您的 ResultSet 定位到它的最后一个元素之后,因为您没有检查它的返回值。

关于java - 结果集未正确定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36605709/

相关文章:

java - 如何在不同的端口上运行@RestController?

java - 使用 Apache Camel 进行 Apache ActiveMQ 窃听

postgresql - 访问列数组并将其存储在新表中

postgresql - Docker:golang webapp 和 postgres 服务器之间的连接被拒绝

MySql结果集合并列

java - 在 Spring Boot 中为多个存储库使用服务类

java - 如何最好地运行数据库后台操作?

ruby-on-rails - 如何在不使用 Heroku 工具的情况下使用 pg_dump?

javascript - 访问结果集中数据类型为 varchar(max) 的字段

java - ResultSet 无法解析为类型