Bonsoir tout le monde; j'essaie de faire une application en java et là je voudrais afficher une liste (en format PDF) d'enregistrements extraits de ma table (mysql) cela est il possible ?? merci d'avance
Bonsoir tout le monde; j'essaie de faire une application en java et là je voudrais afficher une liste (en format PDF) d'enregistrements extraits de ma table (mysql) cela est il possible ?? merci d'avance
Bonsoir,
Oui c'est possible . Pour le projet que j'ai fais j'ai utilisé itextpdf-5.1.3.jar qui te permet de faire cela. Par contre je le trouve moins puissante que le format XSL-FO avec le compilateur FOP qui transforme un fichier XML au format FO en PDF. Donc pour un truc de base tu peux utiliser le premier si tu cherches un peu plus pousser tu peux passer par le XML.
Pour ma part je connais que c'est 2 méthodes . Si quelqu'un d'autre connait une autre méthode.
Pour t'aider : c'est mon code brut je suis désolé pour mon non courage à le mettre en forme ^_^, mais tu peux l'étudier pour voir.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public void pdfFilmStockClient(){ pdfFilm(); /*for (Entry<String, String> film : list_film.entrySet()) { System.out.println(film.getKey()+" >>> "+film.getValue()); }*/ // partie filmStock String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/locavideo"; String user = "root"; String password = ""; com.itextpdf.text.Document document = new com.itextpdf.text.Document(); try { PdfWriter.getInstance(document,new FileOutputStream("Administratif.pdf")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (DocumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } document.open(); try { //initialiser la pdf //la parite produits document.add(new Paragraph(" L'¨¦tat des stock par produit.")); document.add(new Paragraph(" ")); PdfPTable tab = new PdfPTable(7); float one_WidthE[] = { 0.15f, 1.0f, 0.15f, 0.15f, 0.15f, 0.35f, 0.2f}; tab.setWidthPercentage(100); tab.setWidths(one_WidthE); tab.getDefaultCell().setPadding(0); PdfPCell cell = new PdfPCell(new Phrase("Id")); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase("Nom")); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase("Prix")); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase("NbTotal")); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase("NbLocation")); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase("DateAjout")); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase("Etat")); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); // connecter ¨¤ la base Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed()){ System.out.println("Succeeded connecting to the Database!"); } Statement statement = conn.createStatement(); //System.out.println(film.getKey()+" >>> "+film.getValue()); String sql = "SELECT * FROM film"; ResultSet rs = statement.executeQuery(sql); while(rs.next()) { //System.out.println(rs.getString("Film_ID") + "\t" + rs.getString("Film_Prix") + "\t" + rs.getString("Film_NbExemplaireTotal")); cell = new PdfPCell(new Phrase(rs.getString("Film_ID"))); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase(list_film.get(rs.getString("Film_ID")))); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase(rs.getString("Film_Prix"))); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase(rs.getString("Film_NbExemplaireTotal"))); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase(rs.getString("Film_NbExemplaireEnLocation"))); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); cell = new PdfPCell(new Phrase(rs.getString("Film_DateAjout"))); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); String supp; if(rs.getString("Film_Supprime").contentEquals("0")){ supp = "supprim¨¦"; }else{ supp = ""; } cell = new PdfPCell(new Phrase(supp)); cell.setHorizontalAlignment( com.itextpdf.text.Element.ALIGN_CENTER); cell.setVerticalAlignment( com.itextpdf.text.Element.ALIGN_MIDDLE); tab.addCell(cell); } rs.close(); document.add(tab); document.close(); }
L'Etat est bien administré quand l'escalier de l'école est usé et que l'herbe croît sur celui du tribunal.
Modérateur BI
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager