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");
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(); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question} catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); }
} catch (DocumentException de) { de.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); }
conn=mysqlconnect.ConnectDB(); String input ="select * from personne"; pst=conn.prepareStatement(input); rs=pst.executeQuery();
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);
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(); } } }