Search code examples
javamysqlitext

Getting MySQL database table in to a Itext pdf report in Java


I want to connect MySQL database table in to Itext report.I coded but only 1st record of the database table shows in the report. I want to show full database table in the report.

My code and screenshot of the generated report..

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);
        }
    }


} 

Edited : Note : In my database table address and contact info is null for the first supplier

enter image description here


Solution

  • I came up with this solution.. help with previous answers of course ..

    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...");