diff --git a/src/main/java/excel2db/excel2db.java b/src/main/java/excel2db/excel2db.java index 96f6127..350bcbd 100644 --- a/src/main/java/excel2db/excel2db.java +++ b/src/main/java/excel2db/excel2db.java @@ -1,8 +1,10 @@ + package excel2db; import com.ge.mdm.tools.common.ApplicationException; import com.ge.mdm.tools.common.SheetEntityManager; +import excel2db.service.DBConnection; import org.apache.commons.io.FilenameUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -15,21 +17,20 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.File; import java.io.IOException; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Iterator; + public class excel2db { - private static final Logger logger = LoggerFactory.getLogger(excel2db.class); + public static final Logger logger = LoggerFactory.getLogger(excel2db.class); private Workbook workbook; @@ -41,23 +42,9 @@ public class excel2db private String fileExtension; private static Short numberProcessedRecords = Short.valueOf((short)0); - @Value("${db.server}") - String dbServer; - - @Value("${db.user}") - String dbUser; - - @Value("${db.password}") - String dbPassword; + public static Connection connection = null; - @Value("${db.port}") - String dbPort; - - @Value("${db.database}") - String dbDatabase; - - - Connection connection = null; + public DBConnection dbConnection; public excel2db() {} @@ -76,6 +63,10 @@ public void setOutputSheetFile(File outputSheetFile) { this.outputSheetFile = outputSheetFile; } + public void setDbConnection(DBConnection dbConnection) + { + this.dbConnection = dbConnection; + } public static void main(String[] args) { @@ -90,16 +81,24 @@ public static void main(String[] args) try { + ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); + excel2db app = (excel2db)context.getBean("excel2db"); + //DBConnection dbConnection = (DBConnection) context.getBean("dbConnection"); + + app.setInputSheetFile(inputFile); app.initAndValidate(); - app.establishPostgresConnection(); + + app.dbConnection.establishDBConnection(); + app.createTable(); app.populateTable(); app.closeConnections(); + } catch (Exception e) { logger.error("An exception occurred while running application", e); @@ -130,27 +129,6 @@ public void initAndValidate() } - public void establishPostgresConnection() - { - try - { - connection = DriverManager.getConnection( - "jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbDatabase, dbUser, dbPassword) - ; - } - catch (SQLException e) - { - logger.error("Connection Failed! Check output console"); - e.printStackTrace(); - return; - } - if (connection != null) { - logger.info("Postgres connection is established"); - } else { - logger.error("Failed to make connection!"); - } - } - private static OPCPackage openOPCPackage(File file) throws ApplicationException { @@ -189,10 +167,6 @@ private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationE } } - - - - public void createTable() throws SQLException { diff --git a/src/main/java/excel2db/service/DBConnection.java b/src/main/java/excel2db/service/DBConnection.java new file mode 100644 index 0000000..734ddc6 --- /dev/null +++ b/src/main/java/excel2db/service/DBConnection.java @@ -0,0 +1,8 @@ + +package excel2db.service; + +public interface DBConnection { + + public void establishDBConnection(); + +} diff --git a/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java b/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java new file mode 100644 index 0000000..f9ee5d6 --- /dev/null +++ b/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java @@ -0,0 +1,34 @@ +package excel2db.service.impl; + +import excel2db.service.DBConnection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; + +public class DBConnectionOracleImpl implements DBConnection { + + // connection parameters + @Value("${db.server}") + String dbServer; + + @Value("${db.user}") + String dbUser; + + @Value("${db.password}") + String dbPassword; + + @Value("${db.port}") + String dbPort; + + @Value("${db.database}") + String dbDatabase; + + public static final Logger logger = LoggerFactory.getLogger(DBConnectionPostgresImpl.class); + + public void establishDBConnection() { + { + logger.info("Oracle connection is not implemented yet"); + } + } + +} diff --git a/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java b/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java new file mode 100644 index 0000000..bf3a7ba --- /dev/null +++ b/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java @@ -0,0 +1,50 @@ +package excel2db.service.impl; + +import excel2db.excel2db; +import excel2db.service.DBConnection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; + +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DBConnectionPostgresImpl implements DBConnection { + + // connection parameters + @Value("${db.server}") + String dbServer; + + @Value("${db.user}") + String dbUser; + + @Value("${db.password}") + String dbPassword; + + @Value("${db.port}") + String dbPort; + + @Value("${db.database}") + String dbDatabase; + + public static final Logger logger = LoggerFactory.getLogger(DBConnectionPostgresImpl.class); + + public void establishDBConnection() { + try { + excel2db.connection = DriverManager.getConnection( + "jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbDatabase, dbUser, dbPassword) + ; + + } catch (SQLException e) { + logger.error("Connection Failed! Check output console"); + e.printStackTrace(); + return; + } + if (excel2db.connection != null) { + logger.info("Postgres connection is established"); + } else { + logger.error("Failed to make connection!"); + } + } + +} diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index 5f481d9..8e00d33 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -12,6 +12,10 @@ - + + + + + \ No newline at end of file