Skip to content

Instantly share code, notes, and snippets.

@DevYam
Last active July 1, 2023 11:17
Show Gist options
  • Save DevYam/f3065ab3d0eabddb5464374da429a369 to your computer and use it in GitHub Desktop.
Save DevYam/f3065ab3d0eabddb5464374da429a369 to your computer and use it in GitHub Desktop.
Add these dependencies - <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.1.0</version> </dependency> </dependencies>
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class CompareTables {
private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
private static final String USER = "username";
private static final String PASS = "password";
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Workbook workbook = new XSSFWorkbook(); // New Workbook
Sheet sheet = workbook.createSheet("Comparison"); // New Worksheet
int rowNum = 0; // Rows counter
String[] sourceTables = {"SOURCE_TABLE1", "SOURCE_TABLE2"};
String[] archiveTables = {"ARCHIVE_TABLE1", "ARCHIVE_TABLE2"};
// Define the cell style for the title row
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
for (int i = 0; i < sourceTables.length; i++) {
// Create title row
Row titleRow = sheet.createRow(rowNum++);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("Comparison of " + sourceTables[i] + " and " + archiveTables[i]);
titleCell.setCellStyle(titleStyle);
Map<String, ColumnDetail> sourceColumns = getTableColumns(conn, sourceTables[i]);
Map<String, ColumnDetail> archiveColumns = getTableColumns(conn, archiveTables[i]);
// Check for columns in the source table that aren't in the archive table
for (Map.Entry<String, ColumnDetail> entry : sourceColumns.entrySet()) {
String columnName = entry.getKey();
ColumnDetail sourceColumnDetail = entry.getValue();
ColumnDetail archiveColumnDetail = archiveColumns.remove(columnName);
if (archiveColumnDetail == null) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue("Column " + columnName + " missing in archive table.");
} else {
boolean typeMismatch = !sourceColumnDetail.dataType.equals(archiveColumnDetail.dataType);
boolean lengthMismatch = sourceColumnDetail.dataLength != archiveColumnDetail.dataLength;
if (typeMismatch || lengthMismatch) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue("Mismatch found in column: " + columnName);
if (typeMismatch) {
row.createCell(1).setCellValue("Data type: " + sourceColumnDetail.dataType + " / " + archiveColumnDetail.dataType);
}
if (lengthMismatch) {
row.createCell(2).setCellValue("Data length: " + sourceColumnDetail.dataLength + " / " + archiveColumnDetail.dataLength);
}
}
}
}
// If any columns remain in the archive table map, they are extra columns
for (String columnName : archiveColumns.keySet()) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue("Extra column in archive table: " + columnName);
}
rowNum++; // Add an empty row between reports
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
conn.close();
} catch (SQLException | IOException se) {
se.printStackTrace();
}
}
private static Map<String, ColumnDetail> getTableColumns(Connection conn, String tableName) throws SQLException {
Map<String, ColumnDetail> columns = new HashMap<>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT column_name, data_type, data_length FROM all_tab_cols WHERE table_name = '" + tableName + "' AND column_name NOT LIKE 'SYS_%' ORDER BY column_id");
while (rs.next()) {
columns.put(rs.getString("COLUMN_NAME"), new ColumnDetail(rs.getString("DATA_TYPE"), rs.getInt("DATA_LENGTH")));
}
rs.close();
stmt.close();
return columns;
}
public static class ColumnDetail {
String dataType;
int dataLength;
public ColumnDetail(String dataType, int dataLength) {
this.dataType = dataType;
this.dataLength = dataLength;
}
}
}
@DevYam
Copy link
Author

DevYam commented Jul 1, 2023

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment