package org.kodejava.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/kodejava";
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)) {
// Since JDBC 2.0 (JDK 1.2) a scrollable ResultSet was
// introduced to the java.sql API family. Using this
// ResultSet enables us to navigate the result set in
// forward or backward way.
//
// To enable the scrollable ResultSet we need to create
// a statement object by defining the ResultSet type
// (ResultSet.TYPE_SCROLL_SENSITIVE,
// ResultSet.TYPE_SCROLL_INSENSITIVE). If you define the
// ResultSet type to ResultSet.TYPE_FORWARD_ONLY then you
// get a regular ResultSet where you can move forward
// only as in JDBC 1.0
Statement statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// This result set is a scrollable result set
String query = "SELECT * FROM products";
ResultSet resultSet = statement.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Maven dependencies
<!-- 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>
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020