2015-10-05 93 views
0

我正在使用Java将数据库查询写入CSV文件。我该怎么做才能循环查询的结果,以便在CSV文件中逐行显示。以下是我的代码的片段。从数据库查询循环

StringBuilder sb = new StringBuilder(); 

sb.append(reportBO.projectList(this.getSearchBean()); 
sb,append(','); 
sb.append('\n'); 

让说我的查询是

SELECT * FROM PROJECT 

下面是我期望的结果。 enter image description here

+0

写入csv文件的代码在哪里? –

+0

您能否给出您从数据库中查询的数据示例,以及CSV文件中应该显示的内容,它将如何显示? –

+0

PrintWriter pw = new PrintWriter(new File(“test.csv”)); pw.write(sb.toString()); ...这是我如何写入CSV文件,我已经编辑了排队 – watermelon

回答

0

以下是我如何解决我的解决方案。希望它能帮助别人。感谢所有帮助..

for (int i = 0; i < ((List<BaseDTOInf>) getProjectList(searchBean)).size();i++) { 

       BaseDTO list = (BaseDTO) getProjectList(searchBean).get(i); 
       list.getDataMap().get("A"); 
       list.getDataMap().get("B"); 
       list.getDataMap().get("C"); 
       list.getDataMap().get("D"); 
       list.getDataMap().get("E"); 
       list.getDataMap().get("F"); 

       sb.append(list.getDataMap().get("A")); 
       sb.append(','); 
       sb.append(list.getDataMap().get("B")); 
       sb.append(','); 
       sb.append(list.getDataMap().get("C")); 
       sb.append(','); 
       sb.append(list.getDataMap().get("D")); 
       sb.append(','); 
       sb.append(list.getDataMap().get("E")); 
       sb.append(','); 
       sb.append('\n'); 

      } 
1

循环的结果集如下所述,并将其写入到文件,

while (rs.next()){ 
     sb.append(rs.getString(<ColumnName1>)); 
     sb.append(','); 
     sb.append(rs.getString(<ColumnName2>)); 
     sb.append(','); 
     sb.append(rs.getString(<ColumnName3>)); 
     sb.append('\n'); 
    } 

如果你将数据填充到StringBuffer然后确保它被写入到在固定时间间隔的文件(例如,每1000行)。否则,如果结果包含大量行,则可能得到OutOfMemoryException

0

您需要一个CSV编写器来正确处理这个问题。值可以包含逗号,引号,换行符,并且必须正确转义。

从您的代码看来,您似乎正在尝试编写bean,而不是直接从ResultSet中获取东西。在这种情况下,你可以尝试univocity-parsers

//creates a configuration object that allows you to determine how to 
//format your CSV output, among many other things. Check the tutorial. 
CsvWriterSettings settings = new CsvWriterSettings(); 

//Configures the parser to process input objects using the BeanWriterProcessor. 
//This reads the data from your java beans. In this case the type is "TestBean" 
//but in yours it seems that would be something like "SearchBean". 
settings.setRowWriterProcessor(new BeanWriterProcessor<TestBean>(TestBean.class)); 

//Defines the headers to print out to the output. My "TestBean" class has the 
//following fields. Adjust this to reflect the fields in your "SearchBean" 
settings.setHeaders("amount", "pending", "date", "quantity", "comments"); 

// Creates a CsvWriter with the settings above 
CsvWriter writer = new CsvWriter(new FileWriter(new File("/path/to/file.csv")), settings); 

//Loads the beans from the database. 
//The "loadBeanList" method is something you should implement. 
List<TestBean> beansToPersist = loadBeanList(); 

//With a list of beans loaded from the database, you can simply call this 
//and write everything into your CSV file. You can also write beans one by one. 
writer.processRecordsAndClose(beansToPersist); 

披露:我是这个库的作者。它是开放源代码和免费的(Apache V2.0许可证)。

+0

嗨..感谢您指出我的错误:)但是,你能解释更多的解决方案,因为我不太了解它。提前致谢。 @JeronimoBackes – watermelon

+0

为代码添加注释以清楚说明。 –