Skip to content

Commit

Permalink
release temp phase
Browse files Browse the repository at this point in the history
  • Loading branch information
phamvanlinh20111993 committed Sep 17, 2023
1 parent 96108bd commit 5ec2a62
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 79 deletions.
36 changes: 36 additions & 0 deletions scripts-example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
create database fake;

use fake;

create table if not exists employee
(
userId varchar(100) NOT NULL,
name varchar (200),
department varchar(255),
ageName TINYINT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


select * from employee;

truncate table employee;




use fake;
DROP TABLE kiot_viet_price;
create table if not exists kiot_viet_price
(
code varchar(100) NOT NULL,
name varchar (200),
unit varchar(10) default "",
good_group varchar(50),
invent_num double,
cost_price decimal(14, 4),
last_cost_price decimal(14, 4),
market_cost_price decimal(14, 4)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

select * from kiot_viet_price;
truncate kiot_viet_price;
6 changes: 6 additions & 0 deletions src/database/in/handle/AbstractSqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import javax.sql.DataSource;

Expand Down Expand Up @@ -45,6 +46,11 @@ protected void extractEntity(R entity, PreparedStatement statement) throws SQLEx
}

protected void setPrepareStatement(PreparedStatement statement, Object value, int index) throws SQLException {

if(value == null) {
statement.setNull(index, Types.NULL);
}

if (value instanceof String) {
statement.setString(index, value.toString());
return;
Expand Down
15 changes: 14 additions & 1 deletion src/database/in/handle/SimplePrepareStatementSqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SimplePrepareStatementSqlInsert(DataSource dataSource, TransactionIsolati

@Override
public String singleInsertValue(T entity) {

logger.info("SimplePrepareStatementSqlInsert.singleInsertValue() start");
String sqlInsertStatement = this.createInsertPrefixCommand(entity) + SPACE + INSERT_VALUE_KEY + SPACE
+ this.createInsertSuffixCommand(entity);

Expand All @@ -51,6 +51,7 @@ public String singleInsertValue(T entity) {
rs = statement.executeUpdate();
conn.commit();
} catch (SQLException e) {
System.err.println("SimplePrepareStatementSqlInsert.class singleInsertValue(): " + e.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class singleInsertValue(): {}", e.getMessage());
} finally {
try {
Expand All @@ -61,18 +62,25 @@ public String singleInsertValue(T entity) {
conn.close();
}
} catch (SQLException e1) {
System.err.println("SimplePrepareStatementSqlInsert.class singleInsertValue(): " + e1.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class singleInsertValue(): {}", e1.getMessage());
}
}

if (rs > -1) {
return "Inserted " + rs;
}

logger.info("SimplePrepareStatementSqlInsert.singleInsertValue() end");

return "fail single insert";
}

@Override
public String batchInsertValues(List<T> entities, boolean isForceInsert) {

logger.info("SimplePrepareStatementSqlInsert.batchInsertValues() start");

if (entities == null || entities.size() == 0) {
return EMPTY;
}
Expand Down Expand Up @@ -104,9 +112,11 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
try {
conn.rollback();
} catch (SQLException e1) {
System.err.println("SimplePrepareStatementSqlInsert.class batchInsertValues(): " + e1.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
}
System.err.println("SimplePrepareStatementSqlInsert.class batchInsertValues(): " + e.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e.getMessage());
} finally {
try {
Expand All @@ -117,10 +127,13 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
conn.close();
}
} catch (SQLException e1) {
System.err.println("SimplePrepareStatementSqlInsert.class batchInsertValues(): " + e1.getMessage());
logger.error("SimplePrepareStatementSqlInsert.class batchInsertValues(): {}", e1.getMessage());
}
}

logger.info("SimplePrepareStatementSqlInsert.batchInsertValues() ends");

if (res != null) {
return "Inserted " + Utils.sum(res);
}
Expand Down
14 changes: 13 additions & 1 deletion src/database/in/handle/SimpleSqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import javax.sql.DataSource;

import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import database.in.utils.TransactionIsolationLevel;
import database.in.utils.Utils;

public class SimpleSqlInsert<T> extends AbstractSqlInsert<T> {


private static final Logger logger = LoggerFactory.getLogger(SimpleSqlInsert.class);

public SimpleSqlInsert(DataSource dataSource) {
super();
this.dataSource = dataSource;
Expand All @@ -29,6 +33,7 @@ public SimpleSqlInsert(DataSource dataSource, TransactionIsolationLevel transact

@Override
public String singleInsertValue(T entity) {
logger.info("SimpleSqlInsert.singleInsertValue() start");
String insertQuery = this.createInsertPrefixCommand(entity) + SPACE + INSERT_VALUE_KEY + SPACE
+ this.createInsertSuffixCommand(entity);

Expand Down Expand Up @@ -65,12 +70,17 @@ public String singleInsertValue(T entity) {
if (result > 0) {
return "inserted " + result;
}

logger.info("SimpleSqlInsert.singleInsertValue() end");

return "fail single inserted";
}

@Override
public String batchInsertValues(List<T> entities, boolean isForceInsert) {

logger.info("SimpleSqlInsert.batchInsertValues() start");

if (entities == null || entities.size() == 0) {
return EMPTY;
}
Expand Down Expand Up @@ -122,6 +132,8 @@ public String batchInsertValues(List<T> entities, boolean isForceInsert) {
if (isForceInsert) {
return "still insert despite some error";
}

logger.info("SimpleSqlInsert.batchInsertValues() end");

return "fail batch inserted";
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/in/handle/SqlInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface SqlInsert<T> {

public String INSERT_VALUE_KEY = "VALUES";

public String SPACE = " ";
public String SPACE = Constants.BLANK;

public String EMPTY = Constants.EMPTY;

Expand Down
124 changes: 124 additions & 0 deletions src/excel/importer/ExcelImportSimple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package excel.importer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import database.in.handle.SimplePrepareStatementSqlInsert;
import database.in.handle.SqlInsert;
import database.in.utils.TransactionIsolationLevel;
import excel.importer.handle.DefaultTableExcelReader;
import excel.importer.handle.TableExcelReader;
import excel.importer.model.PriceTableKiotVietImportDataModel;
import excel.importer.model.PriceTableKiotVietTableModel;
import utils.ObjectUtils;

public class ExcelImportSimple {

private static final Logger logger = LoggerFactory.getLogger(ExcelImportSimple.class);

private String pathFile;

public ExcelImportSimple(String pathFile) {
this.pathFile = pathFile;
}

/**
*
* @param fileName
* @return
*/
protected Workbook createWorkBook(String fileName) {
try {
InputStream file = new FileInputStream(new File(this.pathFile));
Workbook workbook = pathFile.endsWith("xls") ? new HSSFWorkbook(file) : new XSSFWorkbook(file);
workbook.close();
file.close();

return workbook;
} catch (FileNotFoundException e) {
logger.error("ExcelImportSimple.createWorkBook(): {}", e.getMessage());
} catch (IOException e) {
logger.error("ExcelImportSimple.createWorkBook(): {}", e.getMessage());
}

return null;
}

/**
*
* @return
*/
protected BasicDataSource defaultDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/fake?useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("root");

dataSource.setMinIdle(5);
dataSource.setMaxIdle(10);
dataSource.setMaxTotal(25);
dataSource.setMaxWaitMillis(30000);

return dataSource;
}

/**
*
*/
public void importToDatabase() {

logger.info("ExcelImportSimple.importToDatabase() start");

List<PriceTableKiotVietImportDataModel> models = List.of(new PriceTableKiotVietImportDataModel());

logger.info("ExcelImportSimple.importToDatabase() starting get data from excel");
Workbook workbook = createWorkBook(pathFile);
if (workbook == null)
throw new NullPointerException("Workbook can not be null");

TableExcelReader excelReader = new DefaultTableExcelReader(workbook, models);
List<List<Object>> datas = excelReader.executeImport();

logger.info("ExcelImportSimple.importToDatabase() starting import data from excel to database");
// import to database
SqlInsert<PriceTableKiotVietTableModel> sqlInsertCommand = new SimplePrepareStatementSqlInsert<PriceTableKiotVietTableModel>(
defaultDataSource(), TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ);
// Get data from excel file
for (List<Object> tableRow : datas) {

// work, good point, insert single data
// for (Object row : tableRow) {
// System.out.println("Data " + row.toString());
// PriceTableKiotVietTableModel priceTableKiotVietTableModel = new PriceTableKiotVietTableModel();
// priceTableKiotVietTableModel = ObjectUtils.updateProperties(priceTableKiotVietTableModel, row);
// // import to database
// String result = sqlInsertCommand.singleInsertValue(priceTableKiotVietTableModel);
// System.out.println("Result " + result);
// }

// insert multi data with batch insert
List<PriceTableKiotVietTableModel> kiotVietTableModels = new LinkedList<PriceTableKiotVietTableModel>();
for (Object row : tableRow) {
PriceTableKiotVietTableModel priceTableKiotVietTableModel = new PriceTableKiotVietTableModel();
priceTableKiotVietTableModel = ObjectUtils.updateProperties(priceTableKiotVietTableModel, row);
kiotVietTableModels.add(priceTableKiotVietTableModel);
}
String result = sqlInsertCommand.batchInsertValues(kiotVietTableModels, true);
System.out.println("Result " + result);
}

logger.info("ExcelImportSimple.importToDatabase() end");
}
}
40 changes: 5 additions & 35 deletions src/excel/importer/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,16 @@
package excel.importer.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import excel.importer.handle.DefaultTableExcelReader;
import excel.importer.handle.TableExcelReader;
import main.PriceTableKiotVietDataModel;
import excel.importer.ExcelImportSimple;

public class Main {

public static void main(String[] args) {

String path = "C:\\Users\\DELL\\eclipse-workspace\\SimpleImportExportExcel\\BangGia_KV14082023-213536-263.xlsx";
FileInputStream file;
try {
file = new FileInputStream(new File(path));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);

List<PriceTableKiotVietDataModel> models = List.of(new PriceTableKiotVietDataModel());
TableExcelReader excelImporter = new DefaultTableExcelReader(workbook, models);

List<List<Object>> datas = excelImporter.executeImport();

for (List<Object> tableRow : datas) {
for (Object row : tableRow) {
System.out.println("Data " + row.toString());
}
}

workbook.close();
file.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}

ExcelImportSimple excelImportSimple = new ExcelImportSimple(path);

excelImportSimple.importToDatabase();

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main;
package excel.importer.model;

import java.math.BigDecimal;

Expand All @@ -7,7 +7,6 @@
import excel.importer.annotation.MappingField;
import excel.importer.utils.CellType;
import input.validation.annotation.Max;
import input.validation.annotation.NotEmpty;
import input.validation.annotation.NotNull;
import input.validation.annotation.Size;
import lombok.AllArgsConstructor;
Expand All @@ -17,7 +16,7 @@
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PriceTableKiotVietDataModel {
public class PriceTableKiotVietImportDataModel {

@Size(size = 8)
@NotNull
Expand Down
Loading

0 comments on commit 5ec2a62

Please sign in to comment.