我需要您的帮助来显示从iText中PDFTable中的数据库检索到的收入列表。收入将具有两列,分别是:收入_描述和收入_金额,这些列在称为收入的单独类中定义。检索它们的Java代码是:

List<Earnings> listEarnings = new ArrayList<Earnings>();

try{
    Connection con = Database.getConnection();
    PreparedStatement ps = con.prepareStatement("select * from Earnings");
    List<Earnings> listEarnings = new ArrayList<Earnings>();
    ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Earnings e = new Earnings();
                e.setEarningsDescription(rs.getString("Earning_description"));
                e.setEarningsAmount(rs.getString("Earning_amount"));
                listEarnings.add(e);
    }
catch(Exception e)){
System.out.println("Error");
}


但是,我试图创建一个表以将值放在标题下,但是我需要一些帮助。下面是代码:

PdfPTable table = new PdfPTable(2);
Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
PdfPCell c1 = new PdfPCell(new Phrase("Earning Description"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);

c1 = new PdfPCell(new Phrase("Earning Amount"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);


现在,我需要您的助手在每个标题下添加值。

最佳答案

首先,您没有发布earnings_descriptionearnings_amount的get方法,

因此,假设它们是getEarningsDescription()getEarningsAmount(),但请根据您的Earnings类调整它们:

PdfPTable table = new PdfPTable(2);
Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
PdfPCell c1 = new PdfPCell(new Phrase("Earning Description"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);

c1 = new PdfPCell(new Phrase("Earning Amount"),font);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);

      // Here's how you adding the values
for(int i=0;i<listEarnings.size();i++){



         String temp1 = listEarnings.get(i).getEarningsDescription();
         String temp2 =listEarnings.get(i).getEarningsAmount();


         if(temp.equalsIgnoreCase("")){
             temp="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
         }

         if(temp2.equalsIgnoreCase("")){
            temp2="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
          }
table.addCell( temp1  ); // rows for first column
table.addCell(temp2);  // rows for seconds column

}


注意:不要忘记相应地修改getEarningsDescription()getEarningsAmount()

10-02 03:10