JDBC 中的 TYPE_SCROLL_SENSITIVE ResultSet 是什么?
它表示这是可滚动的 ResultSet,即游标向前或向后移动。这种类型的 ResultSet 对数据库中所做的更改很敏感,即对数据库所做的修改会反映在 ResultSet 中。
这意味着如果我们使用 JDBC 程序与数据库建立连接并检索包含名为 SampleTable 的表中所有记录的 ResultSet,那么如果同时向表中添加了更多记录(在检索 ResultSet 后),这些最新的更改将反映在我们之前获得的 ResultSet 对象中。
下面是一个演示如何创建 TYPE_SCROLL_SENSITIVE 结果集的示例。
示例
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class ScrollSensitive { public static void main(String[] args) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String url = "jdbc:mysql://localhost/testdb"; Connection con = DriverManager.getConnection(url, "root", "password"); //Creating a Statement object Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); stmt.setFetchSize(1); } }
广告