遍历 3 个不同的 JDBC 结果集
Loop through 3 different JDBC resultsets
我正在尝试将来自 3 个不同表格的 3 个 CSV 文件压缩到单个 zip 文件中 your_files_12354627.zip
。
所以,我有以下方法,它可以很好地生成包含 1 个 csv 文件的 zip 文件。我正在尝试对多个文件执行此操作,因此我修改了以下代码(如工作代码下方所示):
下面的工作代码:
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
}
}
修改以下代码:
我应该如何遍历 3 个不同的结果集,以便我可以分别使用 writer.writeAll(resultSetFirst, true);
、writer.writeAll(resultSetSecond, true);
和 writer.writeAll(resultSetThird, true);
?我正在尝试遍历文件名,如下面的代码所示,但不确定我应该如何处理 for 循环内的不同结果集,如下所示。我在我试图弄清楚这个循环的地方发表了评论。
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
}
}
首先,我建议您将代码分解为几个不同的方法。这里有很多重复。
对于您是要为每个文件编写一个 ResultSet,还是为每个文件编写所有三个结果集,我有点困惑。其中一部分是文件 file
、fileFacts
和 fileEncounters
,这些文件在其他任何地方都没有提及。
如果您试图在每个条目中放置一个结果集,那么您可以使用映射将文件映射到结果集,然后遍历条目或键。像....
....
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();
}
}
....
但使用一种方法可能会更容易、更清晰...
....
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();
}
}
这样您就不必创建和填充地图(或列表)只是为了 运行 相同的代码三次。
如果你决定迭代地图来制作东西,你 can/should 仍然使用方法 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());
}
...
寻找类似这样的其他领域来重构方法。一个体面的规则是在每次您认为应该剪切和粘贴代码时考虑创建一个方法。
注意none这段代码实际上已经被编译过或者运行
我正在尝试将来自 3 个不同表格的 3 个 CSV 文件压缩到单个 zip 文件中 your_files_12354627.zip
。
所以,我有以下方法,它可以很好地生成包含 1 个 csv 文件的 zip 文件。我正在尝试对多个文件执行此操作,因此我修改了以下代码(如工作代码下方所示):
下面的工作代码:
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
}
}
修改以下代码:
我应该如何遍历 3 个不同的结果集,以便我可以分别使用 writer.writeAll(resultSetFirst, true);
、writer.writeAll(resultSetSecond, true);
和 writer.writeAll(resultSetThird, true);
?我正在尝试遍历文件名,如下面的代码所示,但不确定我应该如何处理 for 循环内的不同结果集,如下所示。我在我试图弄清楚这个循环的地方发表了评论。
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
}
}
首先,我建议您将代码分解为几个不同的方法。这里有很多重复。
对于您是要为每个文件编写一个 ResultSet,还是为每个文件编写所有三个结果集,我有点困惑。其中一部分是文件 file
、fileFacts
和 fileEncounters
,这些文件在其他任何地方都没有提及。
如果您试图在每个条目中放置一个结果集,那么您可以使用映射将文件映射到结果集,然后遍历条目或键。像....
....
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();
}
}
....
但使用一种方法可能会更容易、更清晰...
....
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();
}
}
这样您就不必创建和填充地图(或列表)只是为了 运行 相同的代码三次。
如果你决定迭代地图来制作东西,你 can/should 仍然使用方法 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());
}
...
寻找类似这样的其他领域来重构方法。一个体面的规则是在每次您认为应该剪切和粘贴代码时考虑创建一个方法。
注意none这段代码实际上已经被编译过或者运行