MySQL Batch insert data implementation code 1th/3 page _java

Source: Internet
Author: User
Tags create database
@echo off
Cls
Set Classpath= ... \api\jogre.jar
Set classpath=%classpath%;.
Set classpath=%classpath%;classes
Set Classpath=%classpath%;lib\dom4j.jar
Java org.jogre.server.JogreServer
Building a table
Copy Code code as follows:

Create Database con_test;
Use Con_test;
CREATE TABLE test (ID int not null,txt varchar (), primary key (ID), index (ID));

For two fields, ID is indexed.
Use Java program to loop the table 100,000 times to insert records, IDS (cycles) and content (this record is the first =xx)
Inserttestmysql.java
Copy Code code as follows:

Import java.lang.*;
Import java.sql.*;
public class inserttestmysql{
public static void Main (String [] args) {
Java.util.Date Now_start = new Java.util.Date ();
Long Start_time=now_start.gettime ();
int st = 100000;
String Str,info;
String db= "Org.gjt.mm.mysql.Driver";
String host= "Jdbc:mysql://192.168.1.35/test";
String user= "root";
String passwd= "root";
Connection Con=null;
try{
Class.forName (db). newinstance ();
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Load driver failure:" +DB);
}
try{
Con=drivermanager.getconnection (HOST,USER,PASSWD);
Con.setautocommit (false);//Close Transaction autocommit
for (int i=1;i<=st;i++) {
info = "This record is the first =";
info = Info.concat (java.lang.Integer.toString (i));
str = "INSERT INTO Test (id,txt) values (?,?);";
PreparedStatement pstmt = con.preparestatement (str);
Pstmt.setint (1,i);
Pstmt.setstring (2,info);
Pstmt.executeupdate ();
}
Con.commit ()//statement completed, submit this transaction
Con.close ();
}
catch (Exception e) {
System.out.println (e);
}
Java.util.Date now_end = new Java.util.Date ();
Long End_time=now_end.gettime ();
Long Use_time=end_time-start_time;
System.out.println ("<<---This page generates time-consuming [" +use_time+ "] milliseconds (" + ((double) use_time)/1000+ "seconds"--->> ");
System.out.println ("\n<<---Insert Record" +st+ "Strip-->>");
}
}

In different versions of JDBC, performance is different.
JDBC 3.1.7, 12770,12778 insert this number when the program exits, Chinese normal.
JDBC 3.1.12 12000 Inserts this number when the program exits, Chinese is normal.
The following prompts are present:
"Exception in Thread" main "Java.lang.OutOfMemoryError:Java heap space"
"Java Heap Space Error" may be my machine memory is not enough. But using the 3.10 series JDBC is normal.
JDBC 3.0.16-ga 100,000 records normal, Chinese is normal.
JDBC 3.0.10 100,000 logged successfully, but Chinese error.
Using the 3.1 series JDBC, the machine's remaining physical memory soon became 40xxKB after the program was run.
This, may also be the jdbc3.1 series requires large memory, my machine memory is not enough.
Go back to your classmate's AMD64 512M RAM machine test tomorrow.
JDBC 3.0.16-ga only this normal, test result is:
Copy Code code as follows:

D:\Program Files\test\db_test>java Inserttestmysql
<<---This page generates time-consuming [98582] milliseconds (98.582 seconds)--->>
<<---Insert record 100,000 records-->>

A few days ago again tested, with open source Jdts JDBC Connection Ms-sql Server SP3 other ibid, test results miserable:
Copy Code code as follows:

D:\dev\java\src\ts\ms-sql>java Inserttestmssql
<<---This page generates time-consuming [1746681] milliseconds (1746.681 seconds)--->>
<<---Insert record 100,000 records-->>

Current 1/3 page 123 Next read the full text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.