Inserting Data
Function Description
The following sample code is used to construct sample data and insert data in batches using the executeBatch () method of PreparedStatement for batchNum times.
The data types are the three fields specified in the created table, which are String, UInt8, and Date.
Sample Code
String insertSql = "insert into " + databaseName + "." + tableName + " values (?,?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertSql); long allBatchBegin = System.currentTimeMillis(); for (int j = 0; j < batchNum; j++) { for (int i = 0; i < batchRows; i++) { preparedStatement.setString(1, "xxx_" + (i + j * 10)); preparedStatement.setInt(2, ((int) (Math.random() * 100))); preparedStatement.setDate(3, generateRandomDate("2018-01-01", "2021-12-31")); preparedStatement.addBatch(); } long begin = System.currentTimeMillis(); preparedStatement.executeBatch(); long end = System.currentTimeMillis(); log.info("Inert batch time is {} ms", end - begin); } long allBatchEnd = System.currentTimeMillis(); log.info("Inert all batch time is {} ms", allBatchEnd - allBatchBegin);
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