JasperReports Tutorial | #1 | Create JapserReport with MySQL Datasource | Code Factory

Code Factory
2 min readNov 10, 2019

--

Donate : Link

Reference Link : Link

Applications : Link

Download Code and Jars : Link

File : GetData.jrxml

<?xml version=”1.0" encoding=”UTF-8"?>
<! — Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1 →
<! — 2017–07–01T18:44:07 →
<jasperReport xmlns=”http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=”http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name=”GetData” pageWidth=”595" pageHeight=”842" columnWidth=”555" leftMargin=”20" rightMargin=”20" topMargin=”20" bottomMargin=”20" uuid=”eeb683ed-9e3f-4319–9935–03916b115500">
<property name=”com.jaspersoft.studio.data.sql.tables” value=”” />
<property name=”com.jaspersoft.studio.data.defaultdataadapter” value=”New Data Adapter “ />
<queryString language=”SQL”>
<![CDATA[select * from emp]]>
</queryString>
<field name=”id” class=”java.lang.Long” />
<field name=”name” class=”java.lang.String” />
<field name=”mob_number” class=”java.lang.String” />
<background>
<band splitType=”Stretch” />
</background>
<title>
<band height=”79" splitType=”Stretch” />
</title>
<pageHeader>
<band height=”35" splitType=”Stretch” />
</pageHeader>
<columnHeader>
<band height=”30" splitType=”Stretch”>
<staticText>
<reportElement x=”40" y=”0" width=”100" height=”30" uuid=”5e40a1c9–9651–4d2d-acf4–777422062937" />
<text>
<![CDATA[id]]>
</text>
</staticText>
<staticText>
<reportElement x=”200" y=”0" width=”100" height=”30" uuid=”e6bf776e-2978–4976–8fbf-1a8b1100dc10" />
<text>
<![CDATA[name]]>
</text>
</staticText>
<staticText>
<reportElement x=”380" y=”0" width=”100" height=”30" uuid=”e8bcd763–2b32–4c11–909f-9d0dd9b35b59" />
<text>
<![CDATA[mob_number]]>
</text>
</staticText>
</band>
</columnHeader>
<detail>
<band height=”50" splitType=”Stretch”>
<textField>
<reportElement x=”40" y=”20" width=”100" height=”30" uuid=”a15cd549-ba59–4510–8220-faf3c8913ccd” />
<textFieldExpression>
<![CDATA[$F{id}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x=”200" y=”20" width=”100" height=”30" uuid=”051bee37–33be-479a-876c-8b3f14010ce4" />
<textFieldExpression>
<![CDATA[$F{name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x=”380" y=”20" width=”100" height=”30" uuid=”d83ca3db-2cb6–499c-abfc-3d6a8beffa55" />
<textFieldExpression>
<![CDATA[$F{mob_number}]]>
</textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height=”45" splitType=”Stretch” />
</columnFooter>
<pageFooter>
<band height=”54" splitType=”Stretch” />
</pageFooter>
<summary>
<band height=”42" splitType=”Stretch” />
</summary>
</jasperReport>

Call JasperReport from Jsp :

File : index.jsp

<%@page import=”java.io.OutputStream”%>
<%@ page contentType=”application/pdf”%>
<%@ page trimDirectiveWhitespaces=”true”%>
<%@page import=”java.util.HashMap”%>
<%@page import=”java.util.Map”%>
<%@page import=”net.sf.jasperreports.engine.design.JRDesignQuery”%>
<%@page import=”net.sf.jasperreports.engine.xml.JRXmlLoader”%>
<%@page import=”net.sf.jasperreports.engine.design.JasperDesign”%>
<%@page import=”net.sf.jasperreports.view.JasperViewer”%>
<%@ page import=”net.sf.jasperreports.engine.*”%>
<%@ page import=”java.io.File”%>
<%@ page import=”java.io.FileInputStream”%>
<%@ page import=”java.io.FileNotFoundException”%>
<%@ page import=”java.io.InputStream”%>
<%@ page import=”java.sql.Connection”%>
<%@ page import=”java.sql.DriverManager”%>
<%@ page import=”java.sql.SQLException”%>
<head>
<title>Insert title here</title>
</head>
<body>
<%
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”, “root”, “root”);
String report = “C:/MyReports/GetData.jrxml”;
JasperReport jasperreport = JasperCompileManager.compileReport(report);
JasperPrint jp = JasperFillManager.fillReport(jasperreport, null, conn);
JasperExportManager.exportReportToPdfStream(jp, response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
out.println(e);
}
%>
</body>
</html>

--

--

No responses yet