codice:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
public class MyReportViewer {
public static String JASPER_REPORT_FOLDER = "/home/fabio/myJava/JTableRowColorTest/src/jtablerowcolortest/";
public static String JASPER_FILENAME = "report1";
public static String DRIVER = "com.mysql.jdbc.Driver";
public static String DB_URL = "jdbc:mysql://localhost:3306/javatest";
public static String DB_NAME = "javatest";
public static String DB_USERNAME = "xxxxx";
public static String DB_PASSWORD = "xxxxx";
public MyReportViewer() throws JRException, ClassNotFoundException, SQLException {
//caricamento file JRXML
JasperDesign jasperDesign = JRXmlLoader.load(JASPER_REPORT_FOLDER + JASPER_FILENAME + ".jrxml");
//compilazione del file e generazione del file JASPER
JasperCompileManager.compileReportToFile(jasperDesign, JASPER_REPORT_FOLDER + JASPER_FILENAME + ".jasper");
//inizializzazione connessione al database
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
Map param = new HashMap();
//rendering e generazione del file PDF
JasperPrint jp = JasperFillManager.fillReport(JASPER_REPORT_FOLDER + JASPER_FILENAME + ".jasper", param, conn);
// Con questa linea ia creazione del PDF mi andava sempre in errore...
//JasperExportManager.exportReportToPdfFile(JASPER_REPORT_FOLDER + JASPER_FILENAME + ".jasper", JASPER_REPORT_FOLDER + "report1.pdf");
//...così l'ho sostituita con questa che mi fa vedere l'anteprima...
JasperViewer viewer = new JasperViewer(jp, true);
viewer.setVisible(true);
}
}
Poi nel codice richiamo la mia classe MyReportViewer per vedere l'anteprima...
codice:
try {
MyReportViewer my_rep = new MyReportViewer();
} catch (JRException ex) {Logger.getLogger(JTableRowColorTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {Logger.getLogger(JTableRowColorTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {Logger.getLogger(JTableRowColorTest.class.getName()).log(Level.SEVERE, null, ex);
}
Domanda:
- Perché quando chiudo la finestra dell'Anteprima-Viewer mi si chiude l'applicazione completa?
- C'è un modo comunque per creare direttamente il file .pdf?