프로젝트

일반

사용자정보

CSV 대용량 데이터 insert 영역 추가 (파일1 DataSource 파일2 DB입력) » readToCsv (1).java

김 미진, 2025/04/22 10:12

 
package com.fr.output;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fr.log.FineLoggerFactory;
import com.fr.schedule.base.bean.output.BaseOutputAction;
import com.fr.schedule.base.constant.ScheduleConstants;
import com.fr.schedule.feature.output.OutputActionHandler;
import com.fr.stable.ArrayUtils;

public class readToCsv extends OutputActionHandler<BaseOutputAction> {
Connection conn ;
PreparedStatement pstm = null;
ResultSet rs = null;
@Override
public void doAction(BaseOutputAction action, Map<String, Object> map) throws Exception {
read();
}
private void read() throws Exception{
//CSV
BufferedReader br2 = null;
String line;
String path = "C:\\SPOT_WP\\TEST4.csv";
List<String[]> resultList = new ArrayList<String[]>();
try {
br2 = new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF-8"));
br2.readLine();
while((line = br2.readLine()) != null) {
String[] temp = line.split("\t"); // 탭으로 구분
for(int i=0; i<temp.length; i++) {
String[] tempStr = temp[i].split(",");
resultList.add(tempStr);
}
}
insert(resultList);
} catch (Exception e) {
FineLoggerFactory.getLogger().error("CSV ERROR");
}
}
private void insert(List<String[]> resultList) throws Exception{
String quary = "INSERT INTO SP_TEST01 "
+ "(KUNNR, NAME1, SORT1,CITY1, BEZEI, KATR1, VTEXT1)"
+ "values (?,?,?,?,?,?,?)";
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(quary);
/******/
for(int i = 0 ; i < resultList.size(); i ++) {
String[] arr = null ;
arr = resultList.get(i);
FineLoggerFactory.getLogger().error("["+i+"]번쨰===");
for(int j = 0 ; j < arr.length; j ++) {
pstm.setString(1, arr[0].replace("\"",""));
pstm.setString(2, arr[1].replace("\"",""));
pstm.setString(3, arr[2].replace("\"",""));
pstm.setString(4, arr[3].replace("\"",""));
pstm.setString(5, arr[4].replace("\"",""));
pstm.setString(6, arr[5].replace("\"",""));
pstm.setString(7, arr[6].replace("\"",""));
}
pstm.addBatch();
pstm.clearParameters();
FineLoggerFactory.getLogger().error("["+i+"]번쨰 끝===");
if( (i % 5000) == 0 ) {
int [] result = pstm.executeBatch();
FineLoggerFactory.getLogger().error("result ==== " + result);
pstm.clearBatch();
conn.commit();
}
}
pstm.executeBatch();
conn.commit();
/******/
} catch (Exception e) {
e.printStackTrace();
}finally {
if(pstm != null) {
try {
pstm.close();
} catch (Exception e2) {
}
if(conn != null) {
try {
conn.close();
} catch (Exception e2) {
}
}
}
}
}


private void get() throws SQLException,Exception {

String quary =
"SELECT COUNT(1) AS USER_COUNT"
+ " FROM SURVEY_ANSWER";
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(quary);
rs = pstm.executeQuery();
int cnt = 9999;
while(rs.next()) {
cnt = Integer.parseInt(rs.getString(1));
}
FineLoggerFactory.getLogger().error("카운트입니다=== " + String.valueOf(cnt));
}
}
(1-1/4)