-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing interface to populate table
- Loading branch information
1 parent
82723e4
commit 2199e69
Showing
5 changed files
with
96 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package excel2db.service; | ||
|
||
import java.sql.SQLException; | ||
|
||
public interface PopulateTable { | ||
|
||
public void populateTable () throws SQLException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,5 +36,4 @@ public void createTable() throws SQLException | |
|
||
} | ||
|
||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package excel2db.service.impl; | ||
|
||
import excel2db.excel2db; | ||
import excel2db.service.PopulateTable; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.Row; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.Iterator; | ||
|
||
public class PopulateTablePostgresImpl implements PopulateTable { | ||
|
||
private String cellStringValue; | ||
|
||
|
||
public void populateTable() | ||
throws SQLException | ||
{ | ||
Iterator<Row> rowIterator = excel2db.sheet.rowIterator(); | ||
|
||
if (rowIterator.hasNext()) { | ||
rowIterator.next(); | ||
} | ||
|
||
|
||
while (rowIterator.hasNext()) | ||
{ | ||
Row row = (Row)rowIterator.next(); | ||
|
||
Short localShort1 = excel2db.numberProcessedRecords;Short localShort2 = excel2db.numberProcessedRecords = Short.valueOf((short)(excel2db.numberProcessedRecords.shortValue() + 1)); | ||
|
||
StringBuilder sqlTableInsertStatement = new StringBuilder(); | ||
sqlTableInsertStatement.append("INSERT INTO excel2db VALUES"); | ||
|
||
sqlTableInsertStatement.append("("); | ||
|
||
|
||
short minColIdx = row.getFirstCellNum(); | ||
short maxColIdx = row.getLastCellNum(); | ||
|
||
for (short colIdx = minColIdx; colIdx < maxColIdx; colIdx = (short)(colIdx + 1)) { | ||
cellStringValue = ""; | ||
Cell cell = row.getCell(colIdx); | ||
if (cell == null) cellStringValue = ""; else cellStringValue = cell.toString(); | ||
sqlTableInsertStatement.append("'" + cellStringValue.replace("'", "''") + "'" + ","); | ||
} | ||
|
||
sqlTableInsertStatement.setLength(sqlTableInsertStatement.length() - 1); | ||
|
||
sqlTableInsertStatement.append(")"); | ||
|
||
PreparedStatement pstmtInsertRow = excel2db.connection.prepareStatement(sqlTableInsertStatement.toString()); | ||
pstmtInsertRow.execute(); | ||
|
||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters