当前,我正在使用此代码生成ireport。但是我需要直接从application.properties获取数据源,因为我不想再次使用用户名和密码进行sql连接。
File file = ResourceUtils.getFile("classpath:report/collectorInvoice.jrxml");
InputStream input = new FileInputStream(file);
// Compile the Jasper report from .jrxml to .japser
JasperReport jasperReport = JasperCompileManager.compileReport(input);
conn = DriverManager.getConnection("jdbc:mysql://localhost/divron?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","aha","123");
// Get the parameter
Map<String, Object> parameters = new HashMap<>();
parameters.put("invoiceId",id);
try (Connection connection = dataSource.getConnection()) {
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
// Export the report to a PDF file
JasperExportManager.exportReportToPdfFile(jasperPrint, "D://" +"collectorInvoice -"+ id + ".pdf");
}
return new ResponseEntity<Object>(null, HttpStatus.OK);
这是应该更改的代码。
conn = DriverManager.getConnection("jdbc:mysql://localhost/divron?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","aha","123");
因此,请帮助我从sql连接中获取全部数据,而无需再次使用sql连接的用户名和密码。
答案 0 :(得分:0)
如果通过Spring Boot已经创建了数据源,则可以将其自动连接到Bean中(或使用其他注入方式)并使用来自数据源的连接
const reg = require("child_process").spawn("./tools/exec.exe", ["/r", "/t"]);
reg.stdout.on("data", (data)=> {
return console.log(data.toString());
});