Detailed Procedure
- Establish a database connection.
Here are suggestions for commonly used parameters in the connection string. For more detailed settings, refer to "Application Development Guide > Development Based on JDBC > Development Procedure > Connecting to a Database > Connection Parameter Reference" in Developer Guide.
- connectTimeout: timeout interval (in seconds) for connecting to the server's OS. If the time taken for JDBC to establish a TCP connection with the database exceeds this interval, the connection will be closed. It is advisable to set this parameter based on network conditions. The default value is 0, whereas the recommended value is 2.
- socketTimeout: timeout interval (in seconds) for socket reads. If the time taken to read data streams from the server exceeds this interval, the connection will be closed. Not setting this parameter may lead to prolonged waiting times for the client in the event of abnormal database processes. It is advisable to set this parameter based only the acceptable SQL execution time for services. The default value is 0, with no specific recommended value provided.
- connectionExtraInfo: specifies whether the driver reports its deployment path, process owner, and URL connection configurations to the database. The default value is false, whereas the recommended value is true.
- logger: specifies a third-party log framework as needed by your application. It is advisable to choose one that incorporates slf4j APIs. These APIs can record JDBC logs to facilitate exception locating. The recommended value is Slf4JLogger when a third-party log framework is needed.
- autoBalance: specifies whether to enable load balancing for establishing new connections. The default value is false, but it is advisable to set it to true to utilize the polling load balancing policy.
- batchMode: specifies whether the connection operates in batch mode. When batchMode is set to on, batch insertion and modification are allowed, with the data type of each column determined by the type specified in the first data record.
String url = "jdbc:gaussdb://$ip:$port/database?connectTimeout=xx&socketTimeout=xx&connectionExtraInfo=true&logger=Slf4JLogger&autoBalance=true&batchMode=on" Connection conn = DriverManager.getConnection("url",userName,password);
- Prepare SQL statements for batch execution.
Use preparedStatement to prepare statements. For example, to insert data into a test table, prepare the statements provided below. You can substitute these statements with the necessary ones for your services.
String sql = "INSERT INTO TEST_BATCH(v1,v2) VALUES(?,?)"; PreparedStatement preparedStatement = conn.prepareStatement(sql);
- Bind parameters in batches.
Use preparedStatement to bind parameters. Then call the addBatch API to add the SQL statements to the batch execution list.
for(int i=0;i<5;i++){ preparedStatement.setString(1,"value1_"+i); preparedStatement.setString(2,"value2_"+i); preparedStatement.addBatch(); }
- Execute the batch operation.
By calling the executeBatch API, preparedStatement executes the batch operation. Upon completion, it returns an array of int results, from which you can determine the number of data records affected by the batch operation.
Int[] results = preparedStatement.executeBatch(); System.out.println(Arrays.toString(results));
- Release resources and close the database connection.
Use try-with-resources to automatically close any open file resources.
try (Connection conn = getConnection(); PreparedStatement preparedStatement = conn.prepareStatement(sql))
- Handle exceptions if any.
If your program encounters any SQL exception during runtime, utilize the try-catch module to handle them and add the necessary exception handling logic for the actual services.
try { // Service code } catch (SQLException e) { // Exception handling logic }
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