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
|
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import com.mysql.jdbc.Driver;
public class test {
public static void main(String[] args) {
// - Paramètres de connexion à la base de données
String url = "jdbc:mysql://localhost/test";
String login = "root";
String password = "*****";
try {
// - Connexion à la base
Driver monDriver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(monDriver);
Connection connection = DriverManager.getConnection(url, login, password);
// - Chargement et compilation du rapport
JasperDesign jasperDesign = JRXmlLoader.load("C:\\Documents and Settings\\B062A8AB\\Bureau\\classic.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
// - Paramètres à envoyer au rapport
Map parameters = new HashMap();
parameters.put("Titre", "Titre");
// - Execution du rapport
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
// - Création du rapport au format PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\Documents and Settings\\B062A8AB\\Bureau\\classic.pdf");
JasperViewer.viewReport(jasperPrint,false);
} catch (JRException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
}
} |
Partager