JavaPythonTech blog contains various tools and skills for a developer. Java/Python Best Practices, Tools, Examples, Programming interview questions.

Generate Oracle insert statements for excel data using java

Many times, it will be easier to generate insert statements using a program rather than creating it manually which will be time consuming especially if you have to create hundreds/thousands of them.

There are many ways to achieve the goal, the easiest being using formulas in excel and generating the insert statements.

It will become more complex to use excel to generate insert statements if the data is in columns rather than rows.

It will be a lot easier to use excel to do this job if the data is in a single row, i.e table columns can be mapped to excel columns.

The below program can be used if the data in excel as given below:



Example : Insert statements should have Row1-Cell1 i.e. 123456 common in 3 insert statements and each should have the values in the columns.
123456,ABC
123456,DEF
123456,GHI
456789,ABC
456789,DEF
456789,GHI
456789,JKL
456789,MNO
.......

Program:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.*;

public class ReadExcelAndGenerateInserts {

    public static void main(String[] args) {
        try {
            Map> map = readExcelFile();
            List insertStatements = createInsertStatements(map);
            createTxtFile(insertStatements);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void createTxtFile(List insertStatements) throws IOException {
        FileWriter fileWriter = new FileWriter("inserts.txt");
        for (int i = 0; i < insertStatements.size(); i++) {
            fileWriter.append(insertStatements.get(i));
            fileWriter.append("\n");
        }
        fileWriter.flush();
        fileWriter.close();
        System.out.println("File created!!");
    }

    private static List createInsertStatements(Map> map) {
        List inserts =  new ArrayList<>();
        Set>> entries = map.entrySet();
            Iterator>> iterator = entries.iterator();
            while(iterator.hasNext()){
                Map.Entry> next = iterator.next();
                int cell1 = next.getKey();
                List value = next.getValue();
                for (int j = 0; j < value.size(); j++) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("INSERT INTO TABLE (COLUMN1,COLUMN2,COLUMN3,COLUMN4,COLUMN5) VALUES (SEQ.Nextval ,");
                    sb.append(cell1).append(",'123','").append(value.get(j)).append("', NULL);");
                    inserts.add(sb.toString());
                }
            }
        System.out.println(inserts);
        return inserts;
    }

    private static Map> readExcelFile() throws IOException {
        Map> map = new HashMap<>();
        File file = new File("Input.xlsx");
        FileInputStream fis = new FileInputStream(file);
        Workbook wb = new XSSFWorkbook(fis);
        Sheet sheet = wb.getSheetAt(0);
        Row row = sheet.getRow(0);
        int noOfCols = row.getLastCellNum();
        System.out.println("No. of columns is : "+noOfCols);
        int key = 0;
        for(int i=0;i            List list = new ArrayList<>();
            for(int rowNumber = 0; rowNumber < sheet.getLastRowNum(); rowNumber++) {
                Row rowNum = sheet.getRow(rowNumber);
                Cell cell = rowNum.getCell(i);
                if(null != cell){
                    if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
                    {
                        if(rowNumber==0){
                            key = (int) cell.getNumericCellValue();
                        }
                        System.out.println(cell.getNumericCellValue());
                    }
                    else if(cell.getCellType() == Cell.CELL_TYPE_STRING)
                    {
                        list.add(cell.getStringCellValue());
                        System.out.println(cell.getStringCellValue());
                    }
                }
            }
            map.put(key,list);
        }
        System.out.println(map);
        return map;
    }
}






















Share:

No comments:

Post a Comment

Announcements

Will be posting twice a week on latest java libraries/frameworks which a developer needs to know in 2019.
Also will add the common errors and resolutions for the same.

Please feel free to comment if you need anything specific.

Recent Posts

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Contributors

Pages