【问题标题】:Jasper Reports 6.7.0 generating report is slowJasper Reports 6.7.0 生成报告很慢
【发布时间】:2019-10-15 07:25:51
【问题描述】:

我正在使用 Jasper Reports 6.7.0,但报告生成速度很慢。

我只为数据库中的一条记录生成报告,但不知道为什么性能不佳。

我提到了一些堆栈溢出的问题,但没有帮助
JasperReports fillReport too slow and resource consuming

我在单击按钮时使用以下代码在 JavaFX 应用程序中生成报告。

@FXML
private void viewReport(ActionEvent e) {
        Followup followup = followupTable.getSelectionModel().getSelectedItem();
        if (followup != null) {
            int fuRprtId = followup.getFuId();
            try {
                FileInputStream fis = new FileInputStream("src/com/homeo/reports/report1.jrxml");
                BufferedInputStream bis = new BufferedInputStream(fis);
                Map map = new HashMap();
                map.put("fuId", fuRprtId);
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/OPD", "root", "");

                JasperReport jasperReport = (JasperReport) JasperCompileManager.compileReport(bis);
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);

                JasperViewer.viewReport(jasperPrint, false);
            } catch (SQLException | ClassNotFoundException | FileNotFoundException | JRException ex) {
                Logger.getLogger(FollowUpController.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        }  

SQL 语法:

SELECT
     followup.`full name` AS followup_full_name,
     followup.`complaints` AS followup_complaints,
     followup.`remedy` AS followup_remedy,
     followup.`fu id` AS followup_fu_id,
     followup.`patientid` AS followup_patientid,
     followup.`date` AS followup_date,
     patient.`age` AS patient_age,
     patient.`address` AS patient_address,
     patient.`ref by` AS patient_ref_by,
     patient.`occupation` AS patient_occupation,
     patient.`diagnosis` AS patient_diagnosis,
     patient.`mother` AS patient_mother,
     patient.`matgrandmother` AS patient_matgrandmother,
     patient.`mat grandfather` AS patient_mat_grandfather,
     patient.`mat uncle` AS patient_mat_uncle,
     patient.`mat aunt` AS patient_mat_aunt,
     patient.`pat uncle` AS patient_pat_uncle,
     patient.`pat aunt` AS patient_pat_aunt,
     patient.`patgrand mother` AS patient_patgrand_mother,
     patient.`pat grandfather` AS patient_pat_grandfather,
     patient.`husband` AS patient_husband,
     patient.`wife` AS patient_wife,
     patient.`children` AS patient_children,
     patient.`brother` AS patient_brother,
     patient.`sister` AS patient_sister,
     patient.`phone` AS patient_phone,
     patient.`gender` AS patient_gender,
     patient.`dob` AS patient_dob,
     patient.`patnt id` AS patient_patnt_id,
     patient.`father` AS patient_father
FROM
     `patient` patient INNER JOIN `followup` followup ON patient.`patnt id` = followup.`patientid` where `fu id` = $P{fuId}  

加载大约需要 8 秒

如何加快速度?

还报告在我构建应用时不加载

【问题讨论】:

  • 你能监控到底是哪一步慢吗?例如。它可能是你的 SQL
  • @Swapnil:要检查慢速是否取决于您的 SQL 查询,您可以编写一个更简单的查询(没有连接或只有一行)并检查填写报告的时间。跨度>
  • 如果不想使用JOIN,则必须对后续表的每个字段使用子查询,但性能代价更高。如果您的 JOIN 很慢,请检查您是否在相关表上添加了正确的索引
  • 您使用的是哪个 DBMS?

标签: java jasper-reports


【解决方案1】:

我很好奇为什么它变慢了。
approx 8 to 9 seconds 太慢了。

但我忽略了它并继续按原样构建,但现在报告无论如何都没有显示。

因为我已经选择了缓慢的问题并且没有帮助。
我更改了我的代码,它解决了我关于 jasper 报告的所有问题。

我不推荐下面的代码,我猜这是老式的,不是正确的做法

   FileInputStream fis = new FileInputStream("src/com/homeo/reports/report1.jrxml");
    BufferedInputStream bis = new BufferedInputStream(fis);
    Map map = new HashMap();
    map.put("fuId", fuRprtId);
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/OPD", "root", "");

    JasperReport jasperReport = (JasperReport) JasperCompileManager.compileReport(bis);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);

    JasperViewer.viewReport(jasperPrint, false);

我使用了下面的代码,它比上面的代码快多了。大约需要 2 seconds 加载

try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/OPD", "root", "")) {
    InputStream inputStream = getClass().getResourceAsStream("/com/homeo/reports/report1.jasper");

    JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map, con);
    JasperViewer.viewReport(jasperPrint, false);
    con.close();  
}
  } catch (SQLException | ClassNotFoundException | JRException ex) {
        Logger.getLogger(FollowUpController.class
                .getName()).log(Level.SEVERE, null, ex);
}  

为了让上面的代码正常工作

您必须使用已编译的
report.jsper 而不是 report.jrfxml
并使用
"/com/reports/report.jsper" 代替 "/src/com/reports/report.jsper"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多