Reputation: 1267
I need your help in displaying the list of earnings which is retrieved from a database in a PDFTable
in iText. The earnings will be having two columns which are: earnings_description and earnings_amount which are defined in a separate class called Earnings. The java code for retrieving them is:
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");
}
However I tried to create a table to place the values under the headers, but I need some help. Below is the code:
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);
Now I need your assistant in adding the values under each header.
Upvotes: 0
Views: 1132
Reputation: 3109
First , you didn't post your get methods for earnings_description
& earnings_amount
,
So assuming they are getEarningsDescription()
& getEarningsAmount()
, but adapt them according to your Earnings class :
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
}
Note : don't forget to adapt those getEarningsDescription()
& getEarningsAmount()
accordingly
Upvotes: 1