我正在研究这个问题超过一个星期我已经使用了两个例子,但没有人为我工作 . 你能帮我在代码中找到错误吗?
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“>
1 回答
过时:我似乎你把文件作为String提供,jasper期望一个URL . 创建一个File对象并从中创建一个URL .
资源还没找到?检查
paiementReportStream
是否为空 .