From c72ddbaf1d17c4444aad0e5aa94d295677bb8287 Mon Sep 17 00:00:00 2001 From: Andrey Bespalov Date: Wed, 21 Jun 2017 23:32:07 +0300 Subject: [PATCH] Add Oracle Implementation --- src/main/java/excel2db/InsertDBRecord.java | 35 ---------- src/main/java/excel2db/excel2db.java | 3 +- .../java/excel2db/service/DBConnection.java | 3 - .../service/impl/CreateTableDerbyImpl.java | 29 +++++++++ .../service/impl/CreateTableOracleImpl.java | 53 ++++++++++++++- .../service/impl/DBConnectionDerbyImpl.java | 65 +++++++++++++++++++ .../service/impl/DBConnectionOracleImpl.java | 41 +++++++++--- .../service/impl/PopulateTableOracleImpl.java | 2 +- src/main/resources/excel2db.properties | 34 +++++++--- src/main/resources/spring.xml | 7 +- src/test/java/InitInputFilesImplTest.java | 6 -- src/test/java/JUnitSuitePopulateTable.java | 3 +- ...plTest.java => PopulateTableImplTest.java} | 8 +-- 13 files changed, 214 insertions(+), 75 deletions(-) delete mode 100644 src/main/java/excel2db/InsertDBRecord.java create mode 100644 src/main/java/excel2db/service/impl/CreateTableDerbyImpl.java create mode 100644 src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java rename src/test/java/{PopulateTablePostgresImplTest.java => PopulateTableImplTest.java} (79%) diff --git a/src/main/java/excel2db/InsertDBRecord.java b/src/main/java/excel2db/InsertDBRecord.java deleted file mode 100644 index 6c7ad4b..0000000 --- a/src/main/java/excel2db/InsertDBRecord.java +++ /dev/null @@ -1,35 +0,0 @@ -package excel2db; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.sql.PreparedStatement; -import java.sql.SQLException; - - -public class InsertDBRecord extends Thread { - - public InsertDBRecord(PreparedStatement insertDBRecordStatement) { - this.insertDBRecordStatement = insertDBRecordStatement; - } - - public PreparedStatement insertDBRecordStatement; - - public static final Logger logger = LoggerFactory.getLogger(InsertDBRecord.class); - - @Override - public void run() { - try { - logger.info("Thread " + getName() + " started"); - insertDBRecordStatement.execute(); - logger.info("Thread " + getName() + " finished"); - } catch (SQLException e) { - //Log the error somehow - } -// catch (InterruptedException e) { -// //Log the error somehow -// } - - } - -} diff --git a/src/main/java/excel2db/excel2db.java b/src/main/java/excel2db/excel2db.java index 2198634..775cf8d 100644 --- a/src/main/java/excel2db/excel2db.java +++ b/src/main/java/excel2db/excel2db.java @@ -62,7 +62,6 @@ public void setInitConstants(InitConstants initConstants) { } - public static void main(String[] args) { try { @@ -80,7 +79,7 @@ public static void main(String[] args) { HashSet fileList = app.generateFileList.generateFileList(); if (fileList == null){ throw new Exception("File list is empty in property file"); - //logger.error("Empty file list"); + } //this way we call methods for objects that is already initialized by the Spring diff --git a/src/main/java/excel2db/service/DBConnection.java b/src/main/java/excel2db/service/DBConnection.java index 9b6a8f4..f178aca 100644 --- a/src/main/java/excel2db/service/DBConnection.java +++ b/src/main/java/excel2db/service/DBConnection.java @@ -4,7 +4,4 @@ public interface DBConnection { public void establishDBConnection(); - interface CreateTable { - public void createTable(); - } } diff --git a/src/main/java/excel2db/service/impl/CreateTableDerbyImpl.java b/src/main/java/excel2db/service/impl/CreateTableDerbyImpl.java new file mode 100644 index 0000000..0d22c99 --- /dev/null +++ b/src/main/java/excel2db/service/impl/CreateTableDerbyImpl.java @@ -0,0 +1,29 @@ +package excel2db.service.impl; + +/** + * Created by Andrey on 6/21/2017. + */ + +import java.sql.SQLException; + +import excel2db.service.CreateTable; +import excel2db.service.PopulateTable; +import org.apache.poi.ss.usermodel.Sheet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class CreateTableDerbyImpl implements CreateTable { + + public static final Logger logger = LoggerFactory.getLogger(PopulateTableOracleImpl.class); + + private Integer numOfProcessedRows = 0; + + public void createTable(Sheet sheet, String tableName) + throws SQLException { + + logger.info("Create table method : Derby implementation is not covered yet"); + + } + +} diff --git a/src/main/java/excel2db/service/impl/CreateTableOracleImpl.java b/src/main/java/excel2db/service/impl/CreateTableOracleImpl.java index 633a033..0e27e07 100644 --- a/src/main/java/excel2db/service/impl/CreateTableOracleImpl.java +++ b/src/main/java/excel2db/service/impl/CreateTableOracleImpl.java @@ -1,6 +1,13 @@ package excel2db.service.impl; +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.LinkedHashMap; +import java.util.Map; + +import excel2db.excel2db; import excel2db.service.CreateTable; import org.apache.poi.ss.usermodel.Sheet; import org.slf4j.Logger; @@ -12,8 +19,52 @@ public class CreateTableOracleImpl implements CreateTable { public void createTable(Sheet sheet, String tableName) throws SQLException { - logger.info("Create table method : Oracle implementation is not covered yet"); + Map header = new LinkedHashMap<>(); + header = InitInputFilesImpl.readSheetHeader(sheet); + + //dropping statement + String sqlTableDropStatement = "DROP TABLE \"" + + tableName + "\""; + + //creating statement + StringBuilder sqlTableCreateStatement = new StringBuilder(); + sqlTableCreateStatement.append("CREATE TABLE \"" + + tableName + "\"("); + for (String headerColumnName : header.keySet()) { + sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " VARCHAR2(20), "); + } + + sqlTableCreateStatement.setLength(sqlTableCreateStatement.length() - 2); + sqlTableCreateStatement.append(")"); + + + //Oracle doesn't support the "DROP IF EXISTS" SQL statement + //Thus, checking the table and dropping if the table exists + logger.info("Checking if the table " + tableName + " already exists"); + DatabaseMetaData dbm = excel2db.connection.getMetaData(); + ResultSet tables = dbm.getTables(null, null, tableName, null); + if (tables.next()) { + // Table exists, dropping + logger.info("The table " + tableName + " exists, dropping ..."); + PreparedStatement pstmtDrop = excel2db.connection.prepareStatement(sqlTableDropStatement); + pstmtDrop.execute(); + logger.info("The table " + tableName + " has been dropped"); + } + else { + // Table does not exist, skipp dropping + logger.info("The table " + tableName + " doesn't exist, skip dropping"); + } + + + //creating + logger.info("The table " + tableName + " is being created ..."); + PreparedStatement pstmtCreate = excel2db.connection.prepareStatement(sqlTableCreateStatement.toString()); + pstmtCreate.execute(); + logger.info("The table " + tableName + " has been created"); + } } + + diff --git a/src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java b/src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java new file mode 100644 index 0000000..c6a0d98 --- /dev/null +++ b/src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java @@ -0,0 +1,65 @@ +package excel2db.service.impl; + +/** + * Created by Andrey on 6/21/2017. + */ + +import java.sql.DriverManager; +import java.sql.SQLException; + +import excel2db.excel2db; +import excel2db.service.DBConnection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; + + + + + +public class DBConnectionDerbyImpl implements DBConnection { + + // connection parameters + @Value("${db.derby.server}") + String dbServer; + + @Value("${db.derby.user}") + String dbUser; + + @Value("${db.derby.password}") + String dbPassword; + + @Value("${db.derby.port}") + String dbPort; + + @Value("${db.derby.database}") + String dbName; + + + public static final Logger logger = LoggerFactory.getLogger(DBConnectionDerbyImpl.class); + + public void establishDBConnection() { + { + + try { + excel2db.connection = DriverManager.getConnection( + //"jdbc:derby//" + dbServer + ":" + dbPort + "/" + dbName + ";create=true;user=" + dbUser + ";password=" + dbPassword + "jdbc:derby://localhost:1527/testDB;create=true" + ); + + } catch (SQLException e) { + logger.error("Derby Connection Failed! Check output console"); + e.printStackTrace(); + return; + } + if (excel2db.connection != null) { + logger.info("Derby connection is established"); + } else { + logger.error("Failed to make the Derby connection!"); + } + + } + + } + +} diff --git a/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java b/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java index 5765272..47d30ab 100644 --- a/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java +++ b/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java @@ -1,34 +1,59 @@ package excel2db.service.impl; +import java.sql.DriverManager; +import java.sql.SQLException; + +import excel2db.excel2db; import excel2db.service.DBConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import oracle.jdbc.driver.OracleDriver; + public class DBConnectionOracleImpl implements DBConnection { // connection parameters - @Value("${db.server}") + @Value("${db.oracle.server}") String dbServer; - @Value("${db.user}") + @Value("${db.oracle.user}") String dbUser; - @Value("${db.password}") + @Value("${db.oracle.password}") String dbPassword; - @Value("${db.port}") + @Value("${db.oracle.port}") String dbPort; - @Value("${db.database}") - String dbDatabase; + //it could be either sid or service name, + // depending what is provided to connect to the DB + @Value("${db.oracle.sid}") + String dbSid; - public static final Logger logger = LoggerFactory.getLogger(DBConnectionPostgresImpl.class); + public static final Logger logger = LoggerFactory.getLogger(DBConnectionOracleImpl.class); public void establishDBConnection() { { - logger.info("Oracle connection is not implemented yet"); + + try { + excel2db.connection = DriverManager.getConnection( + "jdbc:oracle:thin:@//" + dbServer + ":" + dbPort + "/" + dbSid, dbUser, dbPassword) + ; + + } catch (SQLException e) { + logger.error("Oracle Connection Failed! Check output console"); + e.printStackTrace(); + return; + } + if (excel2db.connection != null) { + logger.info("Oracle connection is established"); + } else { + logger.error("Failed to make the Oracle connection!"); + } + } + } } diff --git a/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java b/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java index 21d117d..023d4d9 100644 --- a/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java +++ b/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java @@ -8,7 +8,7 @@ public class PopulateTableOracleImpl implements PopulateTable { - public static final Logger logger = LoggerFactory.getLogger(PopulateTablePostgresImpl.class); + public static final Logger logger = LoggerFactory.getLogger(PopulateTableOracleImpl.class); private Integer numOfProcessedRows = 0; diff --git a/src/main/resources/excel2db.properties b/src/main/resources/excel2db.properties index 48cd326..f3b4131 100644 --- a/src/main/resources/excel2db.properties +++ b/src/main/resources/excel2db.properties @@ -1,12 +1,8 @@ -# names of input tables -input.files=test.xlsx - -# db implementation classes +#db implementation: Postgres, Oracle +db.implementation=Postgres -# postgres section -# dbConnectionImplenentation=excel2db.service.impl.DBConnectionPostgresImpl -# oracle section -dbConnectionImplenentation=excel2db.service.impl.DBConnectionOracleImpl +# names of input tables +input.files=test.xlsx,test1.xlsx # postgres connection parameters db.server=localhost @@ -15,3 +11,25 @@ db.port=5433 db.user=postgres db.password=postgres +# oracle connection parameters +db.oracle.server=localhost +# specify sid or SERVICE NAME here +db.oracle.sid=xe +db.oracle.port=1521 +db.oracle.user=oracle +db.oracle.password=oracle + + +# DON'T TOUCH : + +# set db implementation classes based on the db.implementation +dbConnectionImplenentation=excel2db.service.impl.DBConnection${db.implementation}Impl +createTableImplenentation=excel2db.service.impl.CreateTable${db.implementation}Impl + +# derby connection parameters +db.derby.server=localhost +db.derby.database=excel2db +db.derby.port=5433 +db.derby.user=postgres +db.derby.password=postgres + diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index 3c5f9d1..2379019 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -33,11 +33,12 @@ - - + + - + + diff --git a/src/test/java/InitInputFilesImplTest.java b/src/test/java/InitInputFilesImplTest.java index c2dd7f1..fa8739d 100644 --- a/src/test/java/InitInputFilesImplTest.java +++ b/src/test/java/InitInputFilesImplTest.java @@ -27,12 +27,6 @@ public class InitInputFilesImplTest { public void testInitInputFilesImpl() throws Exception { //initializing application context - //need to check if we really need to get the spring.xml if the config is in "locations" -// AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); -// ApplicationContextUtils appcontext= (ApplicationContextUtils) context.getBean("applicationContextUtils"); -// ApplicationContext appCon = appcontext.getApplicationContext(); -// excel2db app = (excel2db) appCon.getBean("excel2db"); - ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); excel2db app = (excel2db) context.getBean("excel2db"); diff --git a/src/test/java/JUnitSuitePopulateTable.java b/src/test/java/JUnitSuitePopulateTable.java index 58483f3..d09efe6 100644 --- a/src/test/java/JUnitSuitePopulateTable.java +++ b/src/test/java/JUnitSuitePopulateTable.java @@ -2,10 +2,9 @@ import org.junit.runner.RunWith; import org.junit.runners.Suite; - @RunWith(Categories.class) @Categories.IncludeCategory(JUnitPopulateTableCategory.class) -@Suite.SuiteClasses({PopulateTablePostgresImplTest.class}) +@Suite.SuiteClasses({PopulateTableImplTest.class}) public class JUnitSuitePopulateTable { } diff --git a/src/test/java/PopulateTablePostgresImplTest.java b/src/test/java/PopulateTableImplTest.java similarity index 79% rename from src/test/java/PopulateTablePostgresImplTest.java rename to src/test/java/PopulateTableImplTest.java index 9865300..b94257e 100644 --- a/src/test/java/PopulateTablePostgresImplTest.java +++ b/src/test/java/PopulateTableImplTest.java @@ -17,7 +17,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/spring.xml") @Category(JUnitPopulateTableCategory.class) -public class PopulateTablePostgresImplTest { +public class PopulateTableImplTest { private Sheet sheet; @@ -25,16 +25,12 @@ public class PopulateTablePostgresImplTest { public void testPopulateTable() throws Exception { //initializing application context -// AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); -// ApplicationContextUtils appcontext= (ApplicationContextUtils) context.getBean("applicationContextUtils"); -// ApplicationContext appCon =appcontext.getApplicationContext(); -// excel2db app = (excel2db) appCon.getBean("excel2db"); - ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); excel2db app = (excel2db) context.getBean("excel2db"); //executing the test File fileName = new File(app.initConstants.workingDir + "/test.xlsx"); + sheet = app.initInputFiles.initInputFiles(fileName); app.dbConnection.establishDBConnection(); app.createTable.createTable(sheet, "testTable");