操作在结果集关闭后不允许,在 while 循环期间

huangapple go评论80阅读模式
英文:

Operation not allowed after ResultSet closed during while loop

问题

我在while循环期间遇到了Resultset的问题。它给了我一个错误java.sql.SQLException: 在ResultSet关闭后不允许操作,我无法解决这个问题。有人能帮我吗?谢谢!

public static void CandidatesPartyList_JComboBox() {
    try {
        conn1 = VotingSystem.con();
        ps = conn1.prepareStatement("SELECT * FROM partylist WHERE p_status = 'Active' ORDER BY p_name ASC");
        rs = ps.executeQuery();
        candidates_filter_partylist.removeAllItems();
        candidates_filter_partylist.addItem("- 选择党派 -");
        while (rs.next()) { << 问题出现在这里 >>
            candidates_filter_partylist.addItem(rs.getString("p_name"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn1 != null) {
            try {
                conn1.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
英文:

I'm having a problem with Resultset during while loop. It's giving me an error java.sql.SQLException: Operation not allowed after ResultSet closed and I can't figure this out. Can anyone help me out? Thanks!

public static void CandidatesPartyList_JComboBox() {
    try {
        conn1 = VotingSystem.con();
        ps = conn1.prepareStatement(&quot;SELECT * FROM partylist WHERE p_status = &#39;Active&#39; ORDER BY p_name ASC&quot;);
        rs = ps.executeQuery();
        candidates_filter_partylist.removeAllItems();
        candidates_filter_partylist.addItem(&quot;- Select PartyList -&quot;);
        while (rs.next()) { **&lt;&lt; The problem is coming from here**
            candidates_filter_partylist.addItem(rs.getString(&quot;p_name&quot;));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn1 != null) {
            try {
                conn1.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

答案1

得分: 0

你有名为 conn1psrs 的静态变量。静态意味着整个虚拟机中只有一个变量。

很明显,CandidatesPartyList_JComboBox 被不同的线程调用了两次,因此这些变量被重写。

解决方案是:这些变量都不应该是字段(成员变量)。它们应该是局部变量,并且你应该使用 try-with-resources 语句,这将使代码大小减少一半并修复问题。同时,让我们修复错误处理('打印堆栈跟踪' 并不能处理问题,所以不要使用它,更新你的 IDE 模板)。

public static void CandidatesPartyList_JComboBox() {
    try (Connection c = VotingSystem.con();
         PreparedStatement ps = c.prepareStatement("SELECT * FROM partylist WHERE p_status = 'Active' ORDER BY p_name ASC");
         ResultSet rs = ps.executeQuery()) {

        candidates_filter_partylist.removeAllItems();
        candidates_filter_partylist.addItem("- Select PartyList -");
        while (rs.next()) {
            candidates_filter_partylist.addItem(rs.getString("p_name"));
        }
    } catch (SQLException e) {
        throw new RuntimeException("Unhandled", e);
    }
}
英文:

You've got static variables named conn1, ps, and rs. Static means there is only one variable for the entire virtual machine.

Clearly then, CandidatesPartyList_JComboBox is called twice by different threads and thus the variables are overwritten.

The solution is: None of those things should be fields. They should be local variables, and you should be using try-with-resources, which makes this code less than half the size and fixes the problem. Let's also fix the bad error handling ('print the stack trace' is not handling things, so never write that and update your IDE templates).

public static void CandidatesPartyList_JComboBox() {
    try (Connection c = VotingSystem.con();
         PreparedStatement ps = c.prepareStatement(&quot;SELECT * FROM partylist WHERE p_status = &#39;Active&#39; ORDER BY p_name ASC&quot;);
         ResultSet rs = ps.executeQuery()) {

        candidates_filter_partylist.removeAllItems();
        candidates_filter_partylist.addItem(&quot;- Select PartyList -&quot;);
        while (rs.next()) {
            candidates_filter_partylist.addItem(rs.getString(&quot;p_name&quot;));
        }
    } catch (SQLException e) {
        throw new RuntimeException(&quot;Unhandled&quot;, e);
    }
}

huangapple
  • 本文由 发表于 2020年10月26日 17:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64534255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定