简体   繁体   中英

Loop through 3 different JDBC resultsets

I am trying to zip 3 CSV files from 3 different tables in single zip file your_files_12354627.zip .

So, I've the following method which works fine for zip file generation with 1 csv file in it. I'm trying to do this for multiple files and hence I've modified the below code (as shown below the working code):

WORKING CODE BELOW:

public void sendMessage(String msg) throws DaoException {

    DataSource ds = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    PreparedStatement pstmtEmployee = null;
    PreparedStatement pstmtCompany = null; 
    PreparedStatement pstmBuilding = null; 

    ResultSet rs = null;
    ResultSet resultSetFirst = null;
    ResultSet resultSetSecond = null;
    ResultSet resultSetThird = null;


     String[] parts = msg.split("#");
     String requestID = parts[0].trim();
     String userName = parts[1].trim();
     String applicationName = parts[2].trim();




    try {

        ds = jdbcTemplate.getDataSource();
        conn = ds.getConnection();  

        pstmtEmployee = conn.prepareStatement(getPatientEmployeeSQL);
        pstmtEmployee.setString(1, requestID);
        resultSetFirst = pstmtEmployee.executeQuery();

        pstmtCompany = conn.prepareStatement(getCompanySQL);
        pstmtCompany.setString(1, requestID);
        resultSetSecond = pstmtCompany.executeQuery();

        pstmtBuilding = conn.prepareStatement(getBuildingSQL);
        pstmtBuilding.setString(1, requestID);
        resultSetThird = pstmtBuilding.executeQuery();




        Path dir = Paths.get("/srv/custom_users", userName);
        Files.createDirectories(dir);

        OutputStream fos = Files.newOutputStream(dir.resolve("your_files_"+ unixTimestamp +".zip"));
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ZipOutputStream zos = new ZipOutputStream(bos); 

        Path employeeFile = dir.resolve("employee_custom_file_" + unixTimestamp + ".csv");
        Path companyFile = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
        Path buildingFile = dir.resolve("building_custom_file_" + unixTimestamp + ".csv");


        ZipEntry firstEntry = new ZipEntry(employeeFile.getFileName().toString());
        zos.putNextEntry(firstEntry);
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
               writer.writeAll(resultSetFirst, true);
               writer.flush();
               zos.closeEntry();
        }

        /*ZipEntry secondEntry = new ZipEntry(companyFile.getFileName().toString());
        zos.putNextEntry(secondEntry);
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
               writer.writeAll(resultSetSecond, true);
               writer.flush();
               zos.closeEntry();
        }

        ZipEntry thirdEntry = new ZipEntry(buildingFile.getFileName().toString());
        zos.putNextEntry(thirdEntry);
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
               writer.writeAll(resultSetThird, true);
               writer.flush();
               zos.closeEntry();
        }*/




        zos.close();

        }
        catch(Throwable th) {
            throw new DaoException(th.getMessage(), th);
        }
        finally {
            //resource Closing statements

        }   



}

MODIFIED CODE BELOW:

How should I loop through 3 different resultsetsso that I can make use of writer.writeAll(resultSetFirst, true); , writer.writeAll(resultSetSecond, true); and writer.writeAll(resultSetThird, true); respectively? I am trying to loop through the file names as shown in the code below but not sure how should I handle different result sets inside the for loop as shown below. I've commented at the location where I'm trying to figure out this loop thing.

public void sendMessage(String msg) throws DaoException {

    DataSource ds = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    PreparedStatement pstmtEmployee = null;
    PreparedStatement pstmtCompany = null; 
    PreparedStatement pstmBuilding = null; 

    ResultSet rs = null;
    ResultSet resultSetFirst = null;
    ResultSet resultSetSecond = null;
    ResultSet resultSetThird = null;


     String[] parts = msg.split("#");
     String requestID = parts[0].trim();
     String userName = parts[1].trim();
     String applicationName = parts[2].trim();




    try {

        ds = jdbcTemplate.getDataSource();
        conn = ds.getConnection();  

        pstmtEmployee = conn.prepareStatement(getPatientEmployeeSQL);
        pstmtEmployee.setString(1, requestID);
        resultSetFirst = pstmtEmployee.executeQuery();

        pstmtCompany = conn.prepareStatement(getCompanySQL);
        pstmtCompany.setString(1, requestID);
        resultSetSecond = pstmtCompany.executeQuery();

        pstmtBuilding = conn.prepareStatement(getBuildingSQL);
        pstmtBuilding.setString(1, requestID);
        resultSetThird = pstmtBuilding.executeQuery();




        Path dir = Paths.get("/srv/custom_users", userName);
        Files.createDirectories(dir);

        OutputStream fos = Files.newOutputStream(dir.resolve("your_files_"+ unixTimestamp +".zip"));
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ZipOutputStream zos = new ZipOutputStream(bos); 

        Path employeeFile = dir.resolve("employee_custom_file_" + unixTimestamp + ".csv");
        Path companyFile = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
        Path buildingFile = dir.resolve("building_custom_file_" + unixTimestamp + ".csv");


        List<String> csvFileNames = new ArrayList<String>();
             csvFileNames.add(employeeFile.getFileName().toString());
             csvFileNames.add(companyFile.getFileName().toString());
             csvFileNames.add(buildingFile.getFileName().toString());


        for (String entries : csvFileNames) {

                 ZipEntry entry = new ZipEntry(entries);
                 zos.putNextEntry(entry);

                 CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8));
                 //How should I loop through different resultsets here so that I can make use of writer.writeAll(resultSetFirst, true);
                 // writer.writeAll(resultSetSecond, true); and writer.writeAll(resultSetThird, true); respectively?


                System.out.println("Printing entries");
                System.out.println(entries); 

             }   



        /*ZipEntry firstEntry = new ZipEntry(employeeFile.getFileName().toString());
        zos.putNextEntry(firstEntry);
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
               writer.writeAll(resultSetFirst, true);
               writer.flush();
               zos.closeEntry();
        }*/

        /*ZipEntry secondEntry = new ZipEntry(companyFile.getFileName().toString());
        zos.putNextEntry(secondEntry);
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
               writer.writeAll(resultSetSecond, true);
               writer.flush();
               zos.closeEntry();
        }

        ZipEntry thirdEntry = new ZipEntry(buildingFile.getFileName().toString());
        zos.putNextEntry(thirdEntry);
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
               writer.writeAll(resultSetThird, true);
               writer.flush();
               zos.closeEntry();
        }*/




        zos.close();

        }
        catch(Throwable th) {
            throw new DaoException(th.getMessage(), th);
        }
        finally {
            //resource Closing statements

        }   



}

First, I'd suggest you break your code down into several different methods. There's a lot of repetition here.

I'm a bit confused as to whether you're trying to write one ResultSet for each of the files, or all three to each file. A part of this is the files file , fileFacts and fileEncounters which aren't mentioned anywhere else.

If you're trying to put one ResultSet in each Entry, then you could use a Map to map File to ResultSet and then iterate over the Entries or Keys. Something like....

....
  Map<String,ResultSet> dataMap = new HashMap<>();
  dataMap.put(file.getFileName().toString(),resultSetFirst);
  dataMap.put(filefacts.getFileName().toString(),resultSetSecond);
  dataMap.put(fileEncounters.getFileName().toString(),resultSetThird);

  for (Map.Entry<String,ResultSet> e : dataMap.entrySet()){
    zos.putNextEntry(new ZipEntry(e.getKey()));
    try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8))){
      writer.writeAll(e.getValue(), true);
      writer.flush();
      zos.closeEntry();
    }
  }
....

But it'd probably easier and clearer to just use a method...

....
  makeEntry(zos,file.getFileName().toString(),resultSetFirst);
  makeEntry(zos,filefacts.getFileName().toString(),resultSetSecond);
  makeEntry(zos,fileEncounters.getFileName().toString(),resultSetThird);
....
}

private static void makeEntry(ZipOutputStream zos,String name, ResultSet res) throws SomeExceptions{
    zos.putNextEntry(new ZipEntry(name));
    try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8))){
      writer.writeAll(res, true);
      writer.flush();
      zos.closeEntry();
  }
}

This way you don't have to create and populate the map (or list) just to run the same code three times.

You can/should still use a method if you decide to iterate over a map to make things clearer/cleaner:

  Map<String,ResultSet> dataMap = new HashMap<>();
  dataMap.put(file.getFileName().toString(),resultSetFirst);
  dataMap.put(filefacts.getFileName().toString(),resultSetSecond);
  dataMap.put(fileEncounters.getFileName().toString(),resultSetThird);

  for (Map.Entry<String,ResultSet> e : dataMap.entrySet()){
    makeEntry(zos, e.getKey(), e.getValue());
  }
...

Look for other areas like this to refactor into methods. A decent rule is to consider creating a method every time you think you should cut and paste code.

Note that none of this code has actually been compiled or run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM