Java如何创建可滚动的结果集?
package org.nhooo.example.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ScrollableResultSetExample { private static final String URL = "jdbc:mysql://localhost/nhooo"; private static final String USERNAME = "root"; private static final String PASSWORD = ""; public static void main(String[] args) { try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD)) { //从JDBC2.0(JDK1.2)开始,可滚动的ResultSet是 //引入到java.sqlAPI系列。使用这个 //ResultSet使我们能够在以下位置导航结果集 //前进或后退方式。 // //要启用可滚动的ResultSet,我们需要创建 //通过定义ResultSet类型的语句对象 //(ResultSet.TYPE_SCROLL_SENSITIVE, //ResultSet.TYPE_SCROLL_INSENSITIVE)。如果您定义 //ResultSet类型设置为ResultSet.TYPE_FORWARD_ONLY,则您 //获取常规的ResultSet,您可以在其中前进 //仅与JDBC1.0中一样 Statement statement = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //此结果集是可滚动的结果集 String query = "SELECT * FROM products"; ResultSet resultSet = statement.executeQuery(query); } catch (SQLException e) { e.printStackTrace(); } } }
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>