source commit

This commit is contained in:
leejunho 2023-12-22 12:42:12 +09:00
parent 349a5b2f09
commit ab9ac80a20
11 changed files with 521 additions and 0 deletions

11
insert_db_excel.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

BIN
lib/slf4j-api-2.0.7.jar Normal file

Binary file not shown.

176
pom.xml Normal file
View File

@ -0,0 +1,176 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>insertdb</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<repositories>
<repository>
<id>maven-public</id>
<url>http://nexus.iten.co.kr:9999/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MySQL JDBC 드라이버 의존성 -->
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>-->
<dependency>
<groupId>cubrid</groupId>
<artifactId>cubrid-jdbc</artifactId>
<version>11.0.1.0286</version>
<scope>system</scope>
<systemPath>${basedir}/lib/cubrid-jdbc-11.0.1.0286.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!-- json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
</dependency>
<!--<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.35</version>
</dependency>-->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.lib.path>${project.basedir}/lib</project.lib.path>
</properties>
<build>
<plugins>
<!-- Maven 빌드 시 JAR 파일 생성 설정 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>InsertDb</mainClass> <!-- 메인 클래스 지정 -->
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
</plugin>
<!-- Maven 컴파일러 설정 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>

304
src/main/java/InsertDb.java Normal file
View File

@ -0,0 +1,304 @@
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class InsertDb {
final static private Logger logger = LoggerFactory.getLogger(InsertDb.class);
private static final long MIN_PROCESSING_INTERVAL = 1000;
private static final Map<String, Long> lastProcessedTimes = new HashMap<>();
private static final Map<String, Boolean> isFileProcessing = new HashMap<>();
public static void main( String[] args ) {
if(args.length < 1) {
System.out.println("Usage: java InsertDb config.properties");
return;
}
System.out.println("start");
String configFilePath = args[0];
Properties properties = loadProperties(configFilePath);
if(properties.isEmpty()) {
System.out.println("Failed to load properties. Exiting...");
return;
}
String driverClassName = properties.getProperty("db.driverClassName");
String dbUrl = properties.getProperty("db.url");
String dbUser = properties.getProperty("db.username");
String dbPassword = properties.getProperty("db.password");
String folderPath = properties.getProperty("folderPath");
String insertQuery = properties.getProperty("insertQuery");
String ResposeUrl = properties.getProperty("destUrl");
try {
Connection connection = DatabaseConnectionPool.getConnection(driverClassName, dbUrl, dbUser, dbPassword);
connection.createStatement().execute("SET NAMES 'utf8'");
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = new File(folderPath).toPath();
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
System.out.println("Waiting for new files in the folder...");
while(true) {
WatchKey key;
try {
key = watchService.take();
} catch(InterruptedException e) {
System.err.println("Interrupted while waiting for file events.");
return;
}
for(WatchEvent<?> event : key.pollEvents()) {
Path filePath = path.resolve((Path) event.context());
if(event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
String fileName = filePath.toString().toLowerCase();
if((fileName.endsWith(".json") || fileName.endsWith(".txt")) &&
!isFileProcessing.getOrDefault(filePath.toString(), false) &&
shouldProcessFile(filePath.toString())) {
System.out.println("File modification detected json or txt: " + filePath);
isFileProcessing.put(filePath.toString(), true);
waitForFileMove(filePath);
insertDataFromFileToDatabase(connection, filePath.toFile(), insertQuery);
isFileProcessing.put(filePath.toString(), false);
lastProcessedTimes.put(filePath.toString(), System.currentTimeMillis());
} else if(fileName.endsWith(".xlsx") &&
!isFileProcessing.getOrDefault(filePath.toString(), false) &&
shouldProcessFile(filePath.toString())) {
System.out.println("File modification detected xlsx: " + filePath);
waitForFileMove(filePath);
requestDataFromFileToWeb(filePath.toFile(), ResposeUrl);
isFileProcessing.put(filePath.toString(), false);
lastProcessedTimes.put(filePath.toString(), System.currentTimeMillis());
}
}
}
boolean valid = key.reset();
if(!valid) {
System.err.println("WatchKey no longer valid, exiting.");
return;
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
private static Properties loadProperties( String filePath ) {
Properties properties = new Properties();
try(InputStream input = new FileInputStream(filePath)) {
if(input == null) {
System.out.println("Sorry, unable to find " + filePath);
return properties;
}
properties.load(input);
} catch(IOException e) {
e.printStackTrace();
}
return properties;
}
private static boolean shouldProcessFile( String filePath ) {
long currentTime = System.currentTimeMillis();
long lastProcessedTime = lastProcessedTimes.getOrDefault(filePath, 0L);
return (currentTime - lastProcessedTime) >= MIN_PROCESSING_INTERVAL;
}
private static void waitForFileMove( Path filePath ) {
boolean isFileMoved = false;
while(!isFileMoved) {
try {
if(Files.exists(filePath)) {
isFileMoved = true;
System.out.println("File moved: " + filePath);
} else {
Thread.sleep(1000);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
private static void insertDataFromFileToDatabase( Connection connection, File file, String insertQuery ) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
String line;
StringBuilder fileContent = new StringBuilder();
while((line = br.readLine()) != null) {
fileContent.append(line).append("\n");
}
String fileName = file.getName();
String filePath = file.getAbsolutePath();
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, fileName);
preparedStatement.setString(2, filePath);
preparedStatement.setString(3, fileContent.toString());
preparedStatement.executeUpdate();
br.close();
preparedStatement.close();
} catch(Exception e) {
System.err.println("Error inserting data to the database: " + e.getMessage());
}
}
private static void requestDataFromFileToWeb( File file , String ResposeUrl) {
try {
String filePath = file.getPath();
FileInputStream fis = new FileInputStream(filePath);
IOUtils.setByteArrayMaxOverride(Integer.MAX_VALUE);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
//로그 보기
logging(workbook);
fis.close();
String fileName = file.getName();
//데이터
JSONObject jsonObject = getJsonObject(workbook, fileName, filePath);
String apiUrl = ResposeUrl;
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(apiUrl);
httpPost.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
httpPost.addHeader("Content-type", "application/json");
httpPost.addHeader("Accept", "application/json");
HttpResponse response = httpClient.execute(httpPost);
String result = "";
String statusCode = Integer.toString(response.getStatusLine().getStatusCode());
if(statusCode.equals("200")) {
result = EntityUtils.toString(response.getEntity());
result = new String(result.getBytes(StandardCharsets.UTF_8));
JSONParser parser = new JSONParser();
Object obj = parser.parse(result);
JSONObject object = (JSONObject) obj;
System.out.println("result ::: " + result);
} else {
System.out.println("fail !!!!!!!!!!!!!!!!!!");
}
} catch(IOException e) {
e.getMessage();
} catch(ParseException e) {
throw new RuntimeException(e);
} catch(NullPointerException e) {
e.getMessage();
}
}
private static void logging( XSSFWorkbook workbook ) {
//첫번째 시트
System.out.println("=================================");
System.out.println("user_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(1).getCell(2)));
System.out.println("address_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(2).getCell(2)));
System.out.println("phone_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(3).getCell(2)));
System.out.println("mobile_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(3).getCell(4)));
System.out.println("mail_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(4).getCell(2)));
System.out.println("=================================");
System.out.println("user_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(5).getCell(2)));
System.out.println("address_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(6).getCell(2)));
System.out.println("phone_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(7).getCell(2)));
System.out.println("mobile_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(7).getCell(4)));
System.out.println("mail_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(8).getCell(2)));
System.out.println("=================================");
//두번째 시트
System.out.println("=================================");
System.out.println("purpose: {}"+ isNullToString(workbook.getSheetAt(1).getRow(1).getCell(0)));
System.out.println("reason: {}"+ isNullToString(workbook.getSheetAt(1).getRow(3).getCell(0)));
System.out.println("attachment: {}"+ isNullToString(workbook.getSheetAt(1).getRow(5).getCell(0)));
System.out.println("=================================");
}
private static JSONObject getJsonObject( XSSFWorkbook workbook, String fileName , String filePath ) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("fileName", fileName);
jsonObject.put("filePath", filePath);
jsonObject.put("user_1", isNullToString(workbook.getSheetAt(0).getRow(1).getCell(2).toString()));
jsonObject.put("address_1", isNullToString(workbook.getSheetAt(0).getRow(2).getCell(2).toString()));
jsonObject.put("phone_1", isNullToString(workbook.getSheetAt(0).getRow(3).getCell(2).toString()));
jsonObject.put("mobile_1", isNullToString(workbook.getSheetAt(0).getRow(3).getCell(4).toString()));
jsonObject.put("mail_1", isNullToString(workbook.getSheetAt(0).getRow(4).getCell(2).toString()));
jsonObject.put("user_2", isNullToString(workbook.getSheetAt(0).getRow(5).getCell(2).toString()));
jsonObject.put("address_2", isNullToString(workbook.getSheetAt(0).getRow(6).getCell(2).toString()));
jsonObject.put("phone_2", isNullToString(workbook.getSheetAt(0).getRow(7).getCell(2).toString()));
jsonObject.put("mobile_2", isNullToString(workbook.getSheetAt(0).getRow(7).getCell(4).toString()));
jsonObject.put("mail_2", isNullToString(workbook.getSheetAt(0).getRow(8).getCell(2).toString()));
jsonObject.put("purpose", isNullToString(workbook.getSheetAt(1).getRow(1).getCell(0).toString()));
jsonObject.put("reason", isNullToString(workbook.getSheetAt(1).getRow(3).getCell(0).toString()));
jsonObject.put("attachment", isNullToString(workbook.getSheetAt(1).getRow(5).getCell(0).toString()));
return jsonObject;
}
private static String isNullToString( Object object ) {
String string = "";
if(object != null) {
string = object.toString().trim();
}
return string;
}
}
class DatabaseConnectionPool {
private static BasicDataSource dataSource;
public static Connection getConnection( String driverClassName, String dbUrl, String dbUser, String dbPassword ) throws SQLException {
if(dataSource == null) {
dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(dbUrl);
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
}
return dataSource.getConnection();
}
}

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: InsertDb

View File

@ -0,0 +1,12 @@
#folderPath=C:/ePapyrus/insertdb
folderPath=E:/properties/testDir
#db.driverClassName=com.mysql.cj.jdbc.Driver
db.driverClassName=cubrid.jdbc.driver.CUBRIDDriver
#db.url=jdbc:mysql://localhost:3306/insertdb?useUnicode=true&characterEncoding=UTF-8
db.url=jdbc:cubrid:119.193.218.98:8097:kcc_adr_advc:::?althosts=119.193.215.98:8097&ictime=300&charset=utf-8
#db.username=root
db.username=kccadradvcUr
#db.password=epapyrus12#$
db.password=kccadradvc!@#$
insertQuery=INSERT INTO textdata (filename, filepath, filecontents) VALUES (?, ?, ?)
destUrl=http://119.193.215.98:8081/kccadr/textsence/textSenseResponse.do

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: InsertDb

View File

@ -0,0 +1,12 @@
#folderPath=C:/ePapyrus/insertdb
folderPath=E:/properties/testDir
#db.driverClassName=com.mysql.cj.jdbc.Driver
db.driverClassName=cubrid.jdbc.driver.CUBRIDDriver
#db.url=jdbc:mysql://localhost:3306/insertdb?useUnicode=true&characterEncoding=UTF-8
db.url=jdbc:cubrid:119.193.218.98:8097:kcc_adr_advc:::?althosts=119.193.215.98:8097&ictime=300&charset=utf-8
#db.username=root
db.username=kccadradvcUr
#db.password=epapyrus12#$
db.password=kccadradvc!@#$
insertQuery=INSERT INTO textdata (filename, filepath, filecontents) VALUES (?, ?, ?)
destUrl=http://119.193.215.98:8081/kccadr/textsence/textSenseResponse.do