Settings.xml:
mirror1 central mirror1 https://repo.maven.apache.org/maven2/
net.sf.jasperreports jasperreports 6.8.0 jar compile commons-collections commons-collections commons-beanutils commons-beanutils commons-digester commons-digester commons-logging commons-logging commons-digester commons-digester 2.1 compile false mysql mysql-connector-java 5.1.6
package com.mycompany.mavenprojectdemov1; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.xml.JRXmlLoader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * Hello world! * */ public class App { public static Connection getDbaseConnection(String HOST_NAME, String DBASE_NAME, String USER_NAME, String USR_PASS) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); //String connectionURL = "jdbc:mysql://" + hostName + ":3306/" + dbName; String CNTION_URL = "jdbc:mysql://" + HOST_NAME + "/" + DBASE_NAME; Connection conn = DriverManager.getConnection(CNTION_URL, USER_NAME,USR_PASS); return conn; } public static void main( String[] args ) { //Database credential String hostName = "localhost"; String dbName = "springboot"; String userName = "springboot"; String password = "springboot"; //Jasper file location String fileNameJrxml = "E:/Jasper/jasperv1.jrxml"; String fileNamePdf = "E:/Jasper/jasperv1.pdf"; try { //Getting a connection instance Connection connInstance = getDbaseConnection(hostName,dbName,userName,password); System.out.println("Loading the .JRMXML file ...."); JasperDesign jasperDesign = JRXmlLoader.load(fileNameJrxml); System.out.println("Compiling the .JRMXML file to .JASPER file...."); JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); System.out.println("filling parameters to .JASPER file...."); JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(jasperReport, null,connInstance); System.out.println("exporting the JASPER file to PDF file...."); JasperExportManager.exportReportToPdfFile(jprint, fileNamePdf); System.out.println("Successfully completed the export"); } catch (Exception e) { System.out.print("Exception:" + e); } } }
No comments:
Post a Comment