繁体   English   中英

Java:迭代器的怪异问题

[英]Java: weird issue with iterator

我对一个我不太了解的迭代器有一个奇怪的问题。 迭代器在loggingPreparedStatement中使用,但时不时地(约200个中的20个)抛出此错误:

SQLException in executeQuery, errorcode: 0, SQL state: 07001, message: No value specified for parameter 1
while executing statement: com.mysql.jdbc.PreparedStatement@11fffa73: SELECT ac.id as i FROM ac LEFT JOIN pa ON pa.id = ac.adspace_id WHERE  pa.adspace_id IN (** NOT SPECIFIED **) 

这就是我创建迭代器的方式:

private Iterator< Integer > adspacesId = null;

public ArrayList<Integer> getIds(int inAdspaceId) {
        if (inAdspaceId <= 0) return new ArrayList<Integer>();
        ArrayList<Integer> tempList = new ArrayList<Integer>();
        tempList.add(new Integer(inAdspaceId));
        adspacesId = tempList.listIterator();
        num = tempList.size();
        activeOnly = false;

        if (adspacesId != null && adspacesId.hasNext() && num > 0) {
            executeQuery();
        }
        return result;
    }

因此,我将一个int添加到临时arrayList中,然后将其转换为Iterator,然后在查询中使用该Iterator,如下所示:

LoggedPreparedStatement statement = new LoggedPreparedStatement(theQuery.toString());
statement.setInt(1, adspacesId);

因此,我的第一个猜测是,迭代器中没有任何条目,但是它如何通过adspacesId.hasNext()的检查呢?

num的值为1,因此在临时列表中有一个条目,我会假设在迭代器中。

欢迎一点帮助:)

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

Iterator<Integer> it = list.listIterator();

System.out.println("First Element : " + it.next());

我猜您必须调用next()才能获得甚至第一个元素。 我看不到你在这里做了。 而且,如果它是PreparedStatement ,则在执行executeQuery()之前看不到参数的添加位置,并且我建议您在这里使用ListIterator接口而不是Iterator

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM