0
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("VendorOrder.pdf"));
document.open();
Image img = Image.getInstance("ReportHeader.png");
img.setAlignment(Image.MIDDLE);
document.add(img);
String dateOrdered,vendName,vendTotalOrd,vendBalance,vendRebate;
String sql = "select * from VendorOrder where NO=?";
pst = sqliteconn.prepareStatement(sql);
pst.setString(1, getVendorField.getText());
rs = pst.executeQuery();
if(rs.next()) {
dateOrdered = rs.getString("DATE");
vendName = rs.getString("VENDOR");
vendTotalOrd = rs.getString("TOTAL");
vendBalance = rs.getString("BALANCE");
vendRebate = rs.getString("REBATE");
rs.close();
pst.close();
document.add(new Paragraph("\n"));
PdfPTable nameTable = new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Paragraph("CUSTOMER DETAILS"));
cell.setColspan(3);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(BaseColor.CYAN);
nameTable.addCell(cell);
nameTable.setWidths(new int[]{2, 1});
nameTable.addCell("NAME: \n" + vendName);
nameTable.addCell("DATE ORDERED: "+ dateOrdered);
document.add(nameTable);
PdfPTable vendTable = new PdfPTable(3);
PdfPCell cell1 = new PdfPCell(new Paragraph("TOTAL PURCHASED"));
cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
vendTable.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("BALANCE LEFT"));
cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
vendTable.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("TOTAL REBATE"));
cell3.setBackgroundColor(BaseColor.LIGHT_GRAY);
vendTable.addCell(cell3);
vendTable.addCell(vendTotalOrd);
vendTable.addCell(vendBalance);
vendTable.addCell(vendRebate);
document.add(vendTable);
}
document.add(new Paragraph("----------------------------------------------------------------------------------------------------------------------------------"));
document.add(new Paragraph("\n"));
PdfPTable ordTable = new PdfPTable(4);
PdfPCell cell = new PdfPCell(new Paragraph("PURCHASED ITEMS"));
cell.setColspan(4);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(BaseColor.CYAN);
ordTable.addCell(cell);
PdfPCell cell1 = new PdfPCell(new Paragraph("NEWPAPER"));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
ordTable.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("PRICE"));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setBackgroundColor(BaseColor.LIGHT_GRAY);
ordTable.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("# OF ORDERS"));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setBackgroundColor(BaseColor.LIGHT_GRAY);
ordTable.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Paragraph("SUBTOTAL"));
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setBackgroundColor(BaseColor.LIGHT_GRAY);
ordTable.addCell(cell4);
//document.add(ordTable);
String newsName,newsPrice,newsOrders,newsSubt;
String sql1 = "select NEWSPAPER,PRICE,ORDERS,SUBTOTAL from NewspaperOrder where NO=?";
pst = sqliteconn.prepareStatement(sql1);
pst.setString(1, getVendorField.getText());
rs=pst.executeQuery();
while(rs.next()){
newsName = rs.getString("NEWSPAPER");
newsPrice = rs.getString("PRICE");
newsOrders = rs.getString("ORDERS");
newsSubt = rs.getString("SUBTOTAL");
rs.close();
pst.close();
//System.out.println(newsName + " " + newsPrice + " " + newsOrders + " " + newsSubt);
ordTable.addCell(newsName);
ordTable.addCell(newsPrice);
ordTable.addCell(newsOrders);
ordTable.addCell(newsSubt);
}
document.add(ordTable);
document.close();
JOptionPane.showMessageDialog(null, "SAVED");
我試圖從VendorOrder
和NewspaperOrder
數據庫表打印一堆信息iText的PDF。我試圖從VendorOrder
db表中打印一組數據,並且我已經成功將它們打印到PDF,而在NewspaperOrder
db表中,我嘗試打印多於一組數據,並且它僅顯示來自NewspaperOrder
db的第一組數據表。在NewspaperOrder
分貝表打印數據庫表iText的PDF報告的Java
基地,應該打印完所有具有「11」的順序ID的數據。
我看到這個類似的問題和解決方案在某種程度上類似於我做了什麼。我找不到錯誤,因爲代碼不會拋出任何異常。
將rs.close()和pst.close()語句移出循環... –
ohh,謝謝!這解決了這個問題:) :)哈哈 – Mits