Batch Query
In this example, setFetchSize adjusts the memory usage of the client by using the database cursor to obtain server data in batches. It may increase network interaction and affect performance to some extent. The cursor is valid within a transaction. Therefore, disable automatic transaction commit and then manually commit the transaction.
Prerequisites for code running: Add the gaussdbjdbc.jar package as required. For example, if you use an IDE to run code, you need to add the gaussdbjdbc.jar package to the local IDE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
// There will be security risks if the username and password used for authentication are directly written into code. It is recommended that the username and password be stored in the configuration file or environment variables (the password must be stored in ciphertext and decrypted when being used) to ensure security. // In this example, the username and password are stored in environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV in the local environment (set the environment variable names based on the actual situation). // You need to change the values of $ip, $port, and database. import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; public class Batch { public static void main(String[] args) throws SQLException { String driver = "com.huawei.gaussdb.jdbc.Driver"; String username = System.getenv("EXAMPLE_USERNAME_ENV"); String passwd = System.getenv("EXAMPLE_PASSWORD_ENV"); String sourceURL = "jdbc:gaussdb://$ip:$port/database"; Connection conn = null; try { // Load the database driver. Class.forName(driver).newInstance(); } catch (Exception e) { e.printStackTrace(); } try { // Establish a database connection in non-encrypted mode. conn = DriverManager.getConnection(sourceURL, username, passwd); System.out.println("Connection succeed!"); } catch (Exception e) { e.printStackTrace(); } // Disable automatic commit. conn.setAutoCommit(false); // Create a table. Statement st = conn.createStatement(); st.execute("create table mytable (cal1 int);"); // Insert 200 rows of data into the table. PreparedStatement pstm = conn.prepareStatement("insert into mytable values (?)"); for (int i = 0; i < 200; i++) { pstm.setInt(1, i + 1); pstm.addBatch(); } pstm.executeBatch(); conn.commit(); pstm.close(); // Open the cursor and obtain 50 rows of data each time. st.setFetchSize(50); int fetchCount = 0; ResultSet rs = st.executeQuery("SELECT * FROM mytable"); while (rs.next()) { fetchCount++; } System.out.println(fetchCount == 200); conn.commit(); rs.close(); // Disable the server cursor. st.setFetchSize(0); fetchCount = 0; rs = st.executeQuery("SELECT * FROM mytable"); while (rs.next()) { fetchCount++; } System.out.println(fetchCount == 200); conn.commit(); rs.close(); // Close the statement object and database connection. st.close(); conn.close(); } } |
The execution result of the preceding example is as follows:
Connection succeed! true true
Enable automatic commit.
conn.setAutoCommit(true);
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.
For any further questions, feel free to contact us through the chatbot.
Chatbot