Help Center/
GaussDB/
Best Practices/
Best Practices for JDBC/
Best Practices for JDBC (Centralized Instances)/
Streaming Query/
Procedure/
Complete Example
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
- Place gaussdbjdbc.jar and StreamQueryTest.java from Complete Example into the same directory.
- 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
- 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.
Parent topic: Procedure
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot