Updated on 2025-09-04 GMT+08:00

Complete Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StreamQueryTest {
    // Establish a database connection.
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        String driver = "com.huawei.gaussdb.jdbc.Driver";
        // Specify the source URL of the database. (Adjust $ip, $port, and database as needed.) Set the connection parameter enableStreamingQuery to true.
        String sourceURL = "jdbc:gaussdb://$ip:$port/database?enableStreamingQuery=true";
        // Obtain the username and password from the environment variables.
        String userName = System.getenv("EXAMPLE_USERNAME_ENV");
        String password = System.getenv("EXAMPLE_PASSWORD_ENV");
        Class.forName(driver);
        return DriverManager.getConnection(sourceURL, userName, password);
    }

    public static void main(String[] args) {
        String selectSql = "select * from tab_test order by id asc limit ?";
        // Use the try-with-resources syntax to release resources.
        try (Connection conn = getConnection(); PreparedStatement preparedStatement = conn.prepareStatement(selectSql,
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
            preparedStatement.setInt(1, 100000);
            // Set fetchSize to Integer.MIN_VALUE and call executeQuery to query data.
            preparedStatement.setFetchSize(Integer.MIN_VALUE);
            int totalCount = 0;
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    // Process the query results. The sample code below prints only a portion of the query results along with the total number of data records.
                    if (totalCount++ < 5) {
                        System.out.println("row:" + resultSet.getRow() + ",id :" + resultSet.getInt(1));
                    }
                }
                System.out.println("totalCount:" + totalCount);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

Result Verification

  1. Place gaussdbjdbc.jar and StreamQueryTest.java from Complete Example into the same directory.
  2. Compile and execute the sample code. During execution, set the maximum heap memory of JVM to 16 MB.
    javac -classpath ".:gaussdbjdbc.jar" StreamQueryTest.java
    java -Xmx16M -classpath ".:gaussdbjdbc.jar" StreamQueryTest
  3. Run Complete Example. It completes successfully without encountering memory overflow. During the streaming query, the result set stores only one data record at a time. Therefore, the return value of resultSet.getRow() is 1. The execution result of the code is like this:
    row:1,id :1
    row:1,id :2
    row:1,id :3
    row:1,id :4
    row:1,id :5
    totalCount:100000

Rollback Method

  • To disable streaming query for a single statement, delete setFetchSize(Integer.MIN_VALUE) from Statement and PreparedStatement.
  • To disable streaming query for the current connection, set the connection parameter enableStreamingQuery to false.