在 Java 中使用相同的 ResultSet
对象是坏习惯吗?为什么或者为什么不?//这似乎是我唯一的选择,因为我试图查询一个表的记录数,然后查询该表的 View ;我不知道如何更改 View 。
换句话说,就是
ResultSet myResultSet = statement.executeQuery("SELECT count(*) FROM table");
myResultSet.next();
int recordCount = myResultSet.getInt(1);
myResultSet = statement.executeQuery("SELECT * FROM tableView");
//set other variables based on contents of fetched view
一个坏主意?
最佳答案
Is it bad habit to use the same ResultSet object in Java?
我会说这取决于特定的要求和编码方式。
我相信您正在寻找的是可滚动的 ResultSet这是作为 2.0/2.1 版(集成到 JDK 1.2) 中的一项新功能和改进功能引入的,有两种形式:TYPE_SCROLL_INSENSITIVE和 TYPE_SCROLL_SENSITIVE .
如果我们看到您发布的代码:
ResultSet myResultSet = statement.executeQuery("SELECT count(*) FROM table");
myResultSet.next();
int recordCount = myResultSet.getInt(1);
myResultSet = statement.executeQuery("SELECT * FROM tableView");
您基本上所做的是将 myResultSet
引用变量设置为新的 ResultSet
对象。我看不出有什么不好,也许比我更开明的人可以指出。
关于java - 在 Java 中多次使用相同的 ResultSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17484702/