从记录集中获取数据并将其存储在列表中

时间:2014-09-18 06:07:26

标签: java list recordset

我正在尝试阅读并优秀文件并生成pdf报告

我遇到的问题是我能从excel文件中获取记录但无法将其存储在ArrayList / List中

当我打印列表时,它只显示excel文件的最后一条记录

    @ManagedBean
    @RequestScoped
    public class testbean {

    private List<test> Testlist = new ArrayList<test>();

        String loc1 = UploadDownloadFileServlet.location;



    public List<test> getTestlist() throws FilloException {

        test t = new test();

            Fillo fillo = new Fillo();
        Connection connection = fillo.getConnection(loc1);
        String strQuery = "select * from Sheet1";
        Recordset recordset = connection.executeQuery(strQuery);
        System.out.println("path "+loc1);
        int k = recordset.getCount();
        System.out.println(k);
        while (recordset.next()) {
            t.setNAME(recordset.getField("NAME"));
            t.setSURNAME(recordset.getField("SURNAME"));
            t.setCITY(recordset.getField("CITY"));
            System.out.println(i);


                    }
                        Testlist.add(t);
            recordset.close();
        connection.close();


        return Testlist;
    }

    public void setLstPersonas(List<test> Testlist) {
        this.Testlist = Testlist;
    }

    public void exportarPDF(ActionEvent actionEvent) throws JRException, IOException, FilloException{
        File jasper = new File(FacesContext.getCurrentInstance().getExternalContext().getRealPath("/newReport.jasper"));
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasper.getPath(),null, new JRBeanCollectionDataSource(this.getTestlist()));



        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.addHeader("Content-disposition","attachment; filename=jsfReporte.pdf");
        ServletOutputStream stream = response.getOutputStream();

        JasperExportManager.exportReportToPdfStream(jasperPrint, stream);
        stream.flush();
        stream.close();

        FacesContext.getCurrentInstance().responseComplete();           
    }
}

EXCEL中的数据

NAME    SURNAME CITY
JP      SHAH    MUM
RAHUL           BOM
ROHIT   RAUL    DEL
PARTH           BAN
JAY     PIKLE    BEN

PDF格式的输出

NAME SURNAME CITY
JAY PIKLE BEN
JAY PIKLE BEN
JAY PIKLE BEN
JAY PIKLE BEN
JAY PIKLE BEN

3 个答案:

答案 0 :(得分:0)

你放了Testlist.add(t);在你的while循环中

答案 1 :(得分:0)

更改此

while (recordset.next()) {
                t.setNAME(recordset.getField("NAME"));
                t.setSURNAME(recordset.getField("SURNAME"));
                t.setCITY(recordset.getField("CITY"));
                System.out.println(i);


                        }
                            Testlist.add(t);

到这个

while (recordset.next()) {
            t.setNAME(recordset.getField("NAME"));
            t.setSURNAME(recordset.getField("SURNAME"));
            t.setCITY(recordset.getField("CITY"));
            System.out.println(i);

            // this line should be inside the loop
            Testlist.add(t);

                    }

答案 2 :(得分:0)

这是列表的列表。 TestList有多个测试。 不要在公共场合初始化测试,而在将值添加到测试之前在Whileloop中初始化。并将测试添加到whileloop的测试列表中。

我遇到了类似的问题。我的是地图列表。就像您的最后一条记录一样,它覆盖了之前的记录并添加了新记录。当我在while循环中初始化地图时,它就解决了。