Skip to content

Commit

Permalink
Implementing an interface for making db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed Apr 24, 2017
1 parent f14c1c0 commit 5fee93b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 46 deletions.
64 changes: 19 additions & 45 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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() {}

Expand All @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -189,10 +167,6 @@ private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationE
}
}





public void createTable()
throws SQLException
{
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/excel2db/service/DBConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

package excel2db.service;

public interface DBConnection {

public void establishDBConnection();

}
34 changes: 34 additions & 0 deletions src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

}
50 changes: 50 additions & 0 deletions src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java
Original file line number Diff line number Diff line change
@@ -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!");
}
}

}
6 changes: 5 additions & 1 deletion src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<context:component-scan base-package="excel2db" />
<context:property-placeholder location="classpath:excel2db.properties, file:excel2db.properties" ignore-resource-not-found="true"/>

<bean id = "excel2db" class = "excel2db.excel2db"></bean>
<bean id = "excel2db" class = "excel2db.excel2db" scope="singleton">
<property name="dbConnection" ref="dbConnection"></property>
</bean>

<bean id = "dbConnection" class = "excel2db.service.impl.DBConnectionPostgresImpl"></bean>

</beans>

0 comments on commit 5fee93b

Please sign in to comment.