Http://superjavason.iteye.com/blog/255423
JDBC batch insert is mainly used for data import and logging, because logs are usually written under files first.
I used the JDBC driver of MySQL 5.1.5 to test three common methods.
- Method 1: Use preparedstatement to add batch operations
Java code
- Try {
- Class. forname ("com. MySQL. JDBC. Driver ");
- Conn = drivermanager. getconnection (o_url, username, password );
- Conn. setautocommit (false );
- String SQL = "insert adlogs (IP, website, yyyymmdd, hour, object_id) values (?,?,?,?,?) ";
- Preparedstatement Prest = conn. preparestatement (SQL, resultset. type_scroll_sensitive, resultset. concur_read_only );
- For (INT x = 0; x <size; X ++ ){
- Prest. setstring (1, "192.168.1.1 ");
- Prest. setstring (2, "localhost ");
- Prest. setstring (3, "20081009 ");
- Prest. setint (4, 8 );
- Prest. setstring (5, "11111111 ");
- Prest. addbatch ();
- }
- Prest.exe cutebatch ();
- Conn. Commit ();
- Conn. Close ();
- } Catch (sqlexception ex ){
- Logger. getlogger (mylogger. Class. getname (). Log (level. Severe, null, ex );
- } Catch (classnotfoundexception ex ){
- Logger. getlogger (mylogger. Class. getname (). Log (level. Severe, null, ex );
- }
The meanings of the following two parameters during statement:
The first parameter specifies the type of the resultset. The options include:
Type_forward_only: Default type. Only one forward access is allowed and is not affected by changes made to the database by other users.
Type_scroll_insensitive: Allows moving forward or backward in the list, or even specific positioning, such as moving to the Fourth Record in the list or moving two records backward from the current position. It is not affected by changes made by other users to the database.
Type_scroll_sensitive: Allows location in records like type_scroll_insensitive. This type is affected by changes made by other users. If you delete a record after executing the query, the record will disappear from the resultset. Similarly, changes to data values are also reflected in the resultset.
The second parameter sets the concurrency of the resultset. This parameter determines whether the resultset can be updated. The options include:
Concur_read_only: This is the default value and cannot be updated.
Resultset concur_updatable: Indicates that the resultset can be updated.
- Method 2 Use statement to add batches
Java code
- Conn. setautocommit (false );
- Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_read_only );
- For (INT x = 0; x <size; X ++ ){
- Stmt. addbatch ("insert into adlogs (IP, website, yyyymmdd, hour, object_id) values ('2017. 168.1.3 ', 'localhost', '123', 8, '123 ')");
- }
- Stmt.exe cutebatch ();
- Conn. Commit ();
- Method 3: directly use statement
Java code
- Conn. setautocommit (false );
- Statement stmt = conn. createstatement (resultset. type_scroll_sensitive,
- Resultset. concur_read_only );
- For (INT x = 0; x <size; X ++ ){
- Stmt.exe cute ("insert into adlogs (IP, website, yyyymmdd, hour, object_id) values ('2017. 168.1.3 ', 'localhost', '123', 8, '123 ')");
- }
- Conn. Commit ();
The average test time for inserting 0.1 million data entries using the preceding method is as follows:
Method 1:17.844S
Method 2:18.421S
Method 3:16.359S
It can be seen that the insert of JDBC batch statements does not improve the performance, but is slower than the insert without batch. Of course, this may be related to the implementation method of the specific JDBC driver. The attachment contains the test code that can be used to run on your computer.
When executing batch inserts, the most important thing is to automatically submit and cancel the inserts, so no matter whether or not the JDBC batch syntax is used or not.
Java code
- Conn. setautocommit (false)
I personally think the first method is the most convenient and practical.