Friday, May 29, 2020

Dev Tips#43 Connect MYSQL to a Jasper report in Java and JDBC

In this tutorial, I show you how to retrieve data from MySQL to a Jasper Report in Java. JDBC is used to get an instance of the connection and passed to the jasper report engine...
Settings.xml:
   
    <mirrors>
        <mirror>
      <id>mirror1</id>
      <mirrorOf>central</mirrorOf>
      <name>mirror1</name>
      <url>https://repo.maven.apache.org/maven2/</url>
        </mirror>
    </mirrors>
   
  
POM.xml:
   
<dependencies>
        <dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.8.0</version>
    <type>jar</type>
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <artifactId>commons-collections</artifactId>
            <groupId>commons-collections</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-beanutils</artifactId>
            <groupId>commons-beanutils</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-digester</artifactId>
            <groupId>commons-digester</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
<scope>compile</scope>
<optional>false</optional>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>
    </dependencies>
   
  
Main Java Class:
  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