Different ways of performing bulk insert into database from a java application

17,096

Solution 1

I have a demo ,JDBC batch processing file:demo.txt The content

1899942 ,demo1
1899944 ,demo2
1899946 ,demo3
1899948 ,demo4

Insert the data reads the file content

my code:

public class Test2 {
        public static void main(String[] args) {
            long start = System.currentTimeMillis();
            String sql = "insert into mobile_place(number,place) values(?,?)";
            int count=0;
            PreparedStatement pstmt = null;
            Connection conn = JDBCUtil.getConnection();
            try {
                pstmt = conn.prepareStatement(sql);

                InputStreamReader is = new InputStreamReader(new FileInputStream(new File("D:/CC.txt")),"utf-8");
                BufferedReader br = new BufferedReader(is);
                conn.setAutoCommit(false);

                String s1 = null;
                String s2 = null;
                while(br.readLine() != null){
                    count++;
                    String str = br.readLine().toString().trim();
                    s1 = str.substring(0, str.indexOf(","));
                    s2 = str.substring(str.indexOf(",")+1,str.length());

                    pstmt.setString(1, s1);
                    pstmt.setString(2, s2);
                    pstmt.addBatch();

                    if(count%1000==0){
                        pstmt.executeBatch();
                        conn.commit();
                        conn.close();
                        conn = JDBCUtil.getConnection();
                        conn.setAutoCommit(false);
                        pstmt = conn.prepareStatement(sql);
                    }
                    System.out.println("insert "+count+"line");
                }
                if(count%1000!=0){
                    pstmt.executeBatch();
                    conn.commit();
                }
                long end = System.currentTimeMillis();

                System.out.println("Total time spent:"+(end-start));
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    pstmt.close();
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //getConnection()//get jdbc Connection
    public static Connection getConnection(){ 
    try { 
    Class.forName("com.mysql.jdbc.Driver"); 
    } catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
    } 
    try { 
    conn = DriverManager.getConnection(url, userName, password); 
    } catch (SQLException e) { 
    e.printStackTrace(); 
    } 
    return conn; 
    }

Speak for the first time, I hope I can help

I am the demo above use PreparedStatement [Read data calls a PreparedStatement one-off inserted]

JDBC batch There are 3 ways 1.use PreparedStatement demo:

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.executeBatch();   
      conn.commit();   
      conn.close();   
} catch (SQLException ex) {   
   Logger.getLogger(MyLogger.class.getName()).log(Level.SEVERE, null, ex);   
}   

2.use Statement.addBatch methods demo:

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('192.168.1.3', 'localhost','20081009',8,'23123')");   
 }   
stmt.executeBatch();   
conn.commit(); 

3.Direct use of the Statement demo:

conn.setAutoCommit(false);   
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,   
                                    ResultSet.CONCUR_READ_ONLY);   
for(int x = 0; x < size; x++){   
   stmt.execute("INSERT INTO adlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3', 'localhost','20081009',8,'23123')");   
}   
conn.commit(); 

Using the above method Insert the 100000 pieces of data Time consuming: method 1:17.844s method 2:18.421s method 3:16.359s

Solution 2

MS JDBC versions later than 4.1 have SQLServerBulkCopy class that I assume is equivalent to one available in .Net and theoretically it should work as fast as bcp command line utility. https://msdn.microsoft.com/en-us/library/mt221490%28v=sql.110%29.aspx

Solution 3

you can custom your code with JDBC,there is no framework support your requirement

Share:
17,096
Anand Patel
Author by

Anand Patel

Updated on June 08, 2022

Comments

  • Anand Patel
    Anand Patel almost 2 years

    I am looking for different ways of performing bulk insert into database (e.g. SQL Server 2012) from a Java application. I need to insert lot of entities into database very efficiently without making as many calls to database as there are entities.

    My requirement is to perform a bulk insert of entities, where an insert of entity in database could involve inserting data into one or more tables. The following are the two ways which I can think of:

    1. Dynamically generate a batch of SQL statements and execute it against the database by making use of native JDBC support.

    2. Construct XML representation of all the entities and then invoke a stored procedure by passing the generated XML. The stored procedure takes care of parsing the XML and inserting the entities to database.

    I am new to Java and not having enough knowledge of available frameworks. IMO, the above two approaches seems to be very naive and not leveraging the available frameworks. I am requesting experts to share different ways of achieving bulk insert along with its pros and cons. I am open to MyBatis, Spring-MyBatis, Spring-JDBC, JDBC, etc which solves the problem in an efficient manner.

    Thanks.

  • Eran
    Eran about 11 years
    This code seems helpful, and relevant to the question, but I suggest you edit the lines above the code. It's unclear what you meant to say there (demo1, demo2, ...)
  • demo
    demo about 11 years
    JDBC batch There are 2 ways
  • Anand Patel
    Anand Patel about 11 years
    @demo: +1. Also share, if you have any experience with MyBatis or Spring.
  • Roman Marusyk
    Roman Marusyk over 8 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.