Created
November 23, 2012 07:32
-
-
Save MinCha/4134382 to your computer and use it in GitHub Desktop.
Simple JDBC Batch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.naver.my; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import javax.sql.DataSource; | |
import org.apache.commons.lang.StringUtils; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = {"classpath:test.xml"}) | |
public class SqlLogImporter { | |
@Autowired | |
private DataSource dataSource; //MySQL = rewriteBatchedStatements=true | |
@Test | |
public void SQL파일을_Import한다() throws IOException, SQLException { | |
File file = new File("c:\\dev\\math\\sql.txt"); | |
BufferedReader br = new BufferedReader(new FileReader(file)); | |
int total = 0; | |
int i = 0; | |
Connection conn = null; | |
PreparedStatement stmt = null; | |
try { | |
conn = dataSource.getConnection(); | |
conn.setAutoCommit(false); | |
stmt = conn.prepareStatement("INSERT INTO sql_log VALUES (?,?,?)"); | |
String oneLine; | |
while (StringUtils.isNotBlank(oneLine = br.readLine())) { | |
String[] result = convert(oneLine); | |
stmt.setString(1, result[0]); | |
stmt.setDouble(2, Double.valueOf(result[1])); | |
stmt.setString(3, result[2]); | |
stmt.addBatch(); | |
i++; | |
total++; | |
if (i > 10000) { | |
long time = System.currentTimeMillis(); | |
stmt.executeBatch(); | |
conn.commit(); | |
System.out.println(System.currentTimeMillis() - time); | |
System.out.println(total); | |
i = 0; | |
} | |
} | |
} finally { | |
try { | |
stmt.executeBatch(); | |
conn.commit(); | |
} catch (Exception e) { | |
} | |
try { | |
stmt.close(); | |
} catch (Exception e) { | |
} | |
try { | |
conn.close(); | |
} catch (Exception e) { | |
} | |
} | |
} | |
private String[] convert(String line) { | |
String device = line.split(" ")[0]; | |
String time = line.split(" ")[1]; | |
String id = line.replaceAll("^.*?\\s.*?\\s(.*)$", "$1"); | |
return new String[]{device, time, id}; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rewriteBatchedStatements=true <-- For MySql