Hello,

I am using Itext to create reports, I have created the layout which includes a table.

 try{
        Document  doc = new Document();
      PdfWriter.getInstance(doc, new FileOutputStream("Products.pdf"));

      doc.open();
      Image image = Image.getInstance("logo.png");
      //doc.add(new Paragraph("image"));
      doc.add(image);



      doc.add(new Paragraph("Registration",FontFactory.getFont(FontFactory.TIMES_ROMAN,18,Font.BOLD,BaseColor.RED)));
      doc.add(new Paragraph(new Date().toString()));

      doc.add(new Paragraph("------------------------------------------------------------------"));
      PdfPTable table = new PdfPTable(2);
      PdfPCell cell = new PdfPCell(new Paragraph("Teacher name & Stage"));
      cell.setColspan(4);
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      cell.setBackgroundColor(BaseColor.GREEN);
      table.addCell(cell);
      table.addCell("item1");
      table.addCell("item2");
      table.addCell("item3");
      table.addCell("item4");
      doc.add(table);

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

      doc.close();
     }catch(Exception e){
         e.printStackTrace();

     }

I would like to have the values of my table product-tbl(mysql) in the table. How can I do that?

Ok made some changes but nothing happens

 Properties conProps = new Properties();
        conProps.setProperty("user", "root");
        conProps.setProperty("password", "root");  

         try {
               con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/stock", conProps);

        String sql = ("select * from Staff where TID = 1");



            st = (com.mysql.jdbc.PreparedStatement) con.prepareStatement(sql);

             st.executeQuery();

            rs = st.getResultSet();


            while (rs.next()) {

        try{


        Document  doc = new Document();
      PdfWriter.getInstance(doc, new FileOutputStream("Products.pdf"));

      doc.open();
      Image image = Image.getInstance("logo.png");
      //doc.add(new Paragraph("image"));
      doc.add(image);

      doc.add(new Paragraph("Registration",FontFactory.getFont(FontFactory.TIMES_ROMAN,18,Font.BOLD,BaseColor.RED)));
      doc.add(new Paragraph(new Date().toString()));

      doc.add(new Paragraph("------------------------------------------------------------------"));
      PdfPTable table = new PdfPTable(2);
      PdfPCell cell = new PdfPCell(new Paragraph("Teacher name & Stage"));
      cell.setColspan(4);
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      cell.setBackgroundColor(BaseColor.GREEN);
      table.addCell(cell);
      table.addCell(rs.getString("Name"));
      table.addCell(rs.getString("Surname"));
      table.addCell(rs.getString("Hphone"));
      table.addCell(rs.getString("Mphone"));
      doc.add(table);


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

      doc.close();
     }catch(Exception e){
         e.printStackTrace();

     }

            }
        } catch (Exception e) {

JOptionPane.showMessageDialog(null, "Error");
            e.printStackTrace();

        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.