将MySQL数据库表转换为Java中的Itext pdf报告

时间:2015-07-24 05:32:00

标签: java mysql itext

我想将MySQL数据库表连接到Itext report.I编码,但只有数据库表的第一条记录显示在报告中。我想在报告中显示完整的数据库表。

我生成的报告的代码和屏幕截图..

private void backupOKActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int result;

    Date nowdate = new Date(System.currentTimeMillis());  
    Date backday  =  backup_date.getDate();



    if(backday==null){//checking the date is null or not
        JOptionPane.showMessageDialog(null, "Please Enter the Date ...");

    }else if(!backday.before(nowdate)){//checking given date before todays date or not
        JOptionPane.showMessageDialog(null, "Please Enter Date before Todays date...");

    }else{
        try {
            // backup function goes here
            //supplier info backup file fromthe mysql database

            String sql = "Select * from supplierinfo";


            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();

            if(rs.next()){

                String v1 = rs.getString("SupplierID");
                String v2 = rs.getString("SupplierName");
                String v3 = rs.getString("Address");
                String v4 = rs.getString("ContactInfo");


            chooser = new JFileChooser();
            chooser.setCurrentDirectory(new java.io.File("."));
            chooser.setDialogTitle("Save Backup");
            chooser.setApproveButtonText("Save");
            //disables the all filesoptioning here
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            chooser.setAcceptAllFileFilterUsed(false);

            if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
//                System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
//                System.out.print("getSelectedFile() : "+chooser.getSelectedFile());


                // creating the pdf for supplier details using itext

                try {
                    Document pdfsup = new Document();
                    PdfWriter.getInstance(pdfsup, new FileOutputStream(new File(chooser.getSelectedFile(),"Supplier Details Report.pdf")));

                    pdfsup.open();
                    Image imgsup = Image.getInstance("hedder.png");
                    // pdfsup.add(new Paragraph("Suppliers"));
                    pdfsup.add(imgsup);
                    pdfsup.add(new Paragraph("Supplier Details Report",FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLUE)));
                    pdfsup.add(new Paragraph(new Date().toString()));
                    pdfsup.add(new Paragraph("----------------------------------------------------------------------------------------------------------------"));

                    PdfPTable tablesup= new PdfPTable(4);

                    PdfPCell cell = new PdfPCell(new Paragraph("Supplier Information"));
                    cell.setColspan(8);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setBackgroundColor(BaseColor.CYAN);

                    tablesup.addCell(cell);

                    tablesup.addCell("Supplier ID");
                    tablesup.addCell(v1);
                    tablesup.addCell("Supplier Name");
                    tablesup.addCell(v2);
                    tablesup.addCell("Address");
                    tablesup.addCell(v3);
                    tablesup.addCell("Contact Info");
                    tablesup.addCell(v4);
                    pdfsup.add(tablesup);

                    pdfsup.close();

                    JOptionPane.showMessageDialog(null, "Report Saved...");


                } catch (DocumentException ex) {
                    Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else{
                System.out.println("No Selection");
            }
            }
         } catch (SQLException ex) {
            Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


} 

已编辑:注意:在我的数据库中,第一个供应商的地址和联系信息为空

enter image description here

3 个答案:

答案 0 :(得分:2)

您必须使用循环而不是if语句:

      while(rs.next()){

            String v1 = rs.getString("SupplierID");
            String v2 = rs.getString("SupplierName");
            String v3 = rs.getString("Address");
            String v4 = rs.getString("ContactInfo");
            ....

并将所有应该执行的代码放在此循环之外。

答案 1 :(得分:1)

在创建while对象后使用if而不是Document。使用以下代码部分。它应该适合您。

                try {
                    Document pdfsup = new Document();
                    PdfWriter.getInstance(pdfsup, new FileOutputStream(new File(chooser.getSelectedFile(),"Supplier Details Report.pdf")));

                    pdfsup.open();
                    Image imgsup = Image.getInstance("hedder.png");
                    // pdfsup.add(new Paragraph("Suppliers"));
                    pdfsup.add(imgsup);
                    pdfsup.add(new Paragraph("Supplier Details Report",FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLUE)));
                    pdfsup.add(new Paragraph(new Date().toString()));
                    pdfsup.add(new Paragraph("----------------------------------------------------------------------------------------------------------------"));

                    PdfPTable tablesup= new PdfPTable(4);

                    PdfPCell cell = new PdfPCell(new Paragraph("Supplier Information"));
                    cell.setColspan(8);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setBackgroundColor(BaseColor.CYAN);

                    tablesup.addCell("Supplier ID");
                    tablesup.addCell("Supplier Name");
                    tablesup.addCell("Address");
                    tablesup.addCell("Contact Info");

                    tablesup.addCell(cell);
                    while(rs.next()){

                        String v1 = rs.getString("SupplierID");
                        String v2 = rs.getString("SupplierName");
                        String v3 = rs.getString("Address");
                        String v4 = rs.getString("ContactInfo");



                    tablesup.addCell(v1);
                    tablesup.addCell(v2);
                    tablesup.addCell(v3);
                    tablesup.addCell(v4);
                    pdfsup.add(tablesup);
                    } catch (SQLException ex) {
                        //Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    pdfsup.close();

                    JOptionPane.showMessageDialog(null, "Report Saved...");


                } catch (DocumentException ex) {
                   Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
                } catch (FileNotFoundException ex) {
                   Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    //Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
                }

答案 2 :(得分:1)

我提出了这个解决方案..当然,请帮助解决之前的问题。

PdfPTable tablesup= new PdfPTable(4);

                    PdfPCell cell = new PdfPCell(new Paragraph("Supplier Information"));
                    cell.setColspan(8);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setBackgroundColor(BaseColor.CYAN);

                    tablesup.addCell(cell);

                    tablesup.addCell("Supplier ID");
                    tablesup.addCell("Supplier Name");
                    tablesup.addCell("Address");
                    tablesup.addCell("Contact Info");

                while(rs.next()){

                String v1 = rs.getString("SupplierID");
                String v2 = rs.getString("SupplierName");
                String v3 = rs.getString("Address");
                String v4 = rs.getString("ContactInfo");

                tablesup.addCell(v1);
                tablesup.addCell(v2);
                tablesup.addCell(v3);
                tablesup.addCell(v4); 

                }

                pdfsup.add(tablesup);
                pdfsup.close();
                JOptionPane.showMessageDialog(null, "Report Saved...");