【发布时间】:2017-07-28 11:54:09
【问题描述】:
我已经为此工作了一个多星期,我已经使用了 2 个示例,但没有人为我工作。您能帮我找出代码中的错误吗?
Connection conn = null;
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Please include Classpath Where your MySQL Driver is located");
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jwayma_tax2","student","student");
if (conn != null)
{
System.out.println("Database Connected");
} else
{
System.out.println(" connection Failed ");
}
//Parameters as Map to be passed to Jasper
HashMap<String,Object> hmParams=new HashMap<String,Object>();
hmParams.put("dec_id", new Integer(id));
InputStream paiementReportStream= getClass().getResourceAsStream("Jasper/Paiement_db.jrxml");
JasperReport jasperReport= JasperCompileManager.compileReport(paiementReportStream);
JRSaver.saveObject(jasperReport, "Paiement_db.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hmParams, conn);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleOutputStreamExporterOutput out =new SimpleOutputStreamExporterOutput("Paiement_db.pdf");
exporter.setExporterOutput(out);
SimplePdfReportConfiguration reportConfig= new SimplePdfReportConfiguration();
reportConfig.setSizePageToContent(true);
reportConfig.setForceLineBreakPolicy(false);
SimplePdfExporterConfiguration exportConfig = new SimplePdfExporterConfiguration();
exportConfig.setMetadataAuthor("baeldung");
exportConfig.setEncrypted(true);
exportConfig.setAllowedPermissionsHint("PRINTING");
exporter.setConfiguration(reportConfig);
exporter.setConfiguration(exportConfig);
exporter.exportReport();
} catch (Exception sqlExp) {
System.out.printf("Exception::");
sqlExp.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException expSQL) {
System.out.println("SQLExp::CLOSING::" + expSQL.toString());
}
}
我有这个例子的代码 http://www.baeldung.com/spring-jasper 我还尝试了以下示例的代码http://javaonlineguide.net/2015/05/spring-4-jasper-report-integration-example-with-mysql-database-in-eclipse.html
String reportFileName = "Paiement_db";
Connection conn = null;
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Please include Classpath Where your MySQL Driver is located");
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jwayma_tax2","student","student");
if (conn != null)
{
System.out.println("Database Connected");
} else
{
System.out.println(" connection Failed ");
}
//Parameters as Map to be passed to Jasper
HashMap<String,Object> hmParams=new HashMap<String,Object>();
hmParams.put("dec_id", new Integer(id));
JasperReport jasperReport = getCompiledFile(reportFileName, request);
generateReportPDF(response, hmParams, jasperReport, conn);// For PDF report
} catch (Exception sqlExp) {
System.out.printf("Exception::");
sqlExp.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException expSQL) {
System.out.println("SQLExp::CLOSING::" + expSQL.toString());
}
}
这是 template.jrxml
<?xml version="1.0" encoding="UTF-8"?>
http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20 " bottomMargin="20" uuid="31c3a6bf-74ad-4135-99dc-c0a601aa63e1">
【问题讨论】:
-
你有关于 stacktrace 的实际错误消息吗?
-
在第二个例子中,我首先得到一个空白的 pdf 页面,我在这一行 JasperReport jasperReport= JasperCompileManager.compileReport( paiementReportStream);
标签: java mysql spring spring-mvc jasper-reports