【发布时间】:2013-11-08 17:19:43
【问题描述】:
我编写了两个应该填充 GridBagLayout 的 for 循环。不知何故,第二个条件没有起作用,因此我得到了一个例外。
所以程序基本上是做什么的:它从 MySQL 服务器获取数据并将其列在 GridBagLayout 中。因此,我通过使用rs.lenght 和rs.getRow() 来获得行的长度。比我将它复制到 lenghtCounter 用于内部 for 循环(使用 x 变量)。内部 for 循环应在 3 个周期后执行,或者如果 lenghtCounter 等于 0。我在控制台中看到计数器工作并按预期下降到 0,但 for 循环不会中断。相反,它继续并给了我一个例外。
代码如下:
try {
ResultSet rs = MySQL.getStatement().executeQuery(
"select * from obj_house");
rs.last();
int lenghtCounter = rs.getRow();
int lenght = lenghtCounter;
rs.first();
System.out.println(lenghtCounter + " " + (lenght / 3));
for (int y = 0; y <= (lenght / 3); y++) {
for (int x = 0; x < 3 || lenghtCounter==0; x++) {
System.out.println("ID " + lenghtCounter);
JButton street = new JButton("Strasse: " + rs.getString(5));
gbcOut.gridx = x;
gbcOut.gridy = y;
panelBottom.add(street, gbcOut);
System.out.println("X: " + x);
System.out.println("Y: " + y);
rs.next();
lenghtCounter--;
System.out.println("lenght: " + lenghtCounter);
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
【问题讨论】:
-
你能用错误堆栈跟踪更新你的问题吗?
-
java.sql.SQLException: 结果集结束后
标签: java exception for-loop conditional-statements