Extraction de données et la convertir en pdf

zeddman - 31 mars 2014 à 14:23
 zeddman - 2 avril 2014 à 16:13
bonjour,
j'ai pas passé mon temps à rechercher, d'extraire mon données dans ma table en pdf avec itext et de l'imprimer !!! là j'ai vraiment besoin d'aide!
ok merci bcp
A voir également:

8 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
2 avril 2014 à 10:16
Voilà un code qui marche :

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/javabdd";
String user = "root";
String passwd = "";
Connection conn = DriverManager.getConnection(url, user, passwd);
Statement state = conn.createStatement();
ResultSet result = state.executeQuery("SELECT * FROM personne");
5
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
1 avril 2014 à 07:55
Et la trace ?
(Le message d'erreur généré par printStackTrace())
2
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.*;
import javax.sql.*;
/**
 *
 * @author 2mi
 */
public class createPDF {
Connection conn=null;
ResultSet rs=null;
PreparedStatement pst=null;
    public static void main(String[] args) {
 
        //  Assume the below is the input file format
      conn=mysqlconnect.ConnectDB();
      String input ="select * from personne";
      pst=conn.prepareStatement(input);       
      rs=pst.executeQuery();
      
        // creation of a document-object
        Document document = new Document();
        try {
            // create a writer
            PdfWriter.getInstance(
            // that listens to the document
            document,
            // and directs a PDF-stream to a file
            new FileOutputStream("output.pdf"));
            // open document
            document.open();
            // ajouter table dans le document
            PdfPTable table = new PdfPTable(5);
            PdfPCell cell =
                new PdfPCell(
                    new Paragraph("entete"));                
            cell.setColspan(5);
           // cell.setBackgroundColor(Color.red);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
 
            //Définir le texte d'en-tête de la Table

            cell = new PdfPCell(new Paragraph("id"));
            //cell.setBackgroundColor(Color.blue);
            table.addCell(cell);
 
            cell = new PdfPCell(new Paragraph("nom"));
           // cell.setBackgroundColor(Color.blue);
            table.addCell(cell);
 
            cell = new PdfPCell(new Paragraph("prenom"));
            //cell.setBackgroundColor(Color.blue);
            table.addCell(cell);
 
            cell = new PdfPCell(new Paragraph("matricule"));
           // cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("age"));
            //cell.setBackgroundColor(Color.blue);
            table.addCell(cell);
 
            //Fill data to the table                       
 
            String inputArray[] = input.split("END");
 
            for (int i = 0; i < inputArray.length; i++) {
 
                String fieldValuesArray[] = inputArray[i].split(",");
 
                for (int j = 0; j < fieldValuesArray.length; j++) {
 
                    table.addCell(fieldValuesArray[j].toString());
 
                }
            }
 
            document.add(table);
        } catch (DocumentException de) {
            System.err.println(de.getMessage());
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
 
        // close the document
        document.close();
 
    }
}



je fait comme ça mais ça marche pas!!! ma requête ne marche pas
1
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
31 mars 2014 à 14:45
Salut,

Qu'est ce qui te poses problème ? Tu as une erreur ?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
31 mars 2014 à 15:46
Remplaces
   } catch (DocumentException de) {
            System.err.println(de.getMessage());
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }


par

   } catch (DocumentException de) {
            de.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }


Et donnes moi la trace de l'erreur, je te dirai ce qui ne va pas.
0
Mon erreur c'est ici
      conn=mysqlconnect.ConnectDB();
      String input ="select * from personne";
      pst=conn.prepareStatement(input);      
      rs=pst.executeQuery();

 
0
bonjour,mon probleme est exactement l'execution de ma requête
try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            dbConnect =   DriverManager.getConnection("jdbc:mysql://localhost/javabdd","root","");
            dbStatement = dbConnect.createStatement();
            ResultSet res = dbStatement.execute    Query(input);


il me colore en rougeau dessous de mon code!!! aide moi stp!! je suis vraiment en galère en ce moment!!!! merci d'avance!!



 import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.DriverManager;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.sql.*;
/**
 *
 * @author 2mi
 */
public class createPDF {
//Connection conn=null;
//ResultSet rs=null;
//PreparedStatement pst=null;
     Connection dbConnect = null;
     Statement dbStatement = null;
    public static void main(String[] args) {
        Connection dbConnect = null;
        Statement dbStatement = null;
        String input = "select * from personne";
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            dbConnect = DriverManager.getConnection("jdbc:mysql://localhost/javabdd","root","");
            dbStatement = dbConnect.createStatement();
            ResultSet res = dbStatement.execute    Query(input);
        //  Assume the below is the input file format
     // conn=mysqlconnect.ConnectDB();
      //String input ="select * from personne";
      //pst=conn.prepareStatement(input);
      //rs=pst.executeQuery();

        // creation of a document-object
        Document document = new Document();
          try {
            // create a writer
            PdfWriter.getInstance(
            // that listens to the document
            document,
            // and directs a PDF-stream to a file
            new FileOutputStream("output.pdf"));
            // open document
            document.open();
            // ajouter table dans le document
            PdfPTable table = new PdfPTable(5);
            PdfPCell cell =
                new PdfPCell(
                    new Paragraph("entete"));
            cell.setColspan(5);
           // cell.setBackgroundColor(Color.red);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            //Définir le texte d'en-tête de la Table

            cell = new PdfPCell(new Paragraph("id"));
            //cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("nom"));
           // cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("prenom"));
            //cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("matricule"));
           // cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("age"));
            //cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            //Fill data to the table

            String inputArray[] = input.split("END");

            for (int i = 0; i < inputArray.length; i++) {

                String fieldValuesArray[] = inputArray[i].split(",");

                for (int j = 0; j < fieldValuesArray.length; j++) {

                    table.addCell(fieldValuesArray[j].toString());

                }
            }

            document.add(table);
        } catch (DocumentException de) {
            de.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        // close the document
        document.close();
        }
 
    }
}
0
salut! attend ,je vais tester
0
Rejoignez-nous