Monday, November 26, 2012

Simle Java Program To generate Report through Jasper Report and MS-SQL Server

I am assuming ,you have created a JRXML or JASPER file with Query inside the File by iReport designer .
Mandatory Jar for this program!
(1)jasperreports-4.5.0.jar
(2)sqljdbc.jar(If you are using Java6 please use sqljdc4.jar)
(3)commons-digester.jar
(4)commons-logging.jar
(5)commons-collections.jar
(6)commons-benutils.jar
(7)commons-fileupload.jar


public void showReport() throws JRException, SQLException,
   ClassNotFoundException {
  Connection conn = null;
  try {

   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   conn = DriverManager
     .getConnection("jdbc:sqlserver://localhost:1444;
databaseName=db;user=name;password=password");
   InputStream input = getClass().getClassLoader()
     .getResourceAsStream("DynamicWithQuery.jrxml");

   JasperDesign jasperDesign = JRXmlLoader.load(input);
   JasperReport jasperReport = JasperCompileManager
     .compileReport(jasperDesign);
   Map parameters = new HashMap();
   parameters.put("userId", "SA");
   JasperPrint jasperPrint = JasperFillManager.fillReport(
     jasperReport, parameters, conn);

   JasperViewer.viewReport(jasperPrint);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (conn != null)
    conn.close();
  }

 }
And here is jrxml file
<?xml version="1.0" encoding="UTF-8"?>

<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="DynamicWithQuery" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="userId" class="java.lang.String">
<defaultValueExpression><![CDATA[$F{ValidUserOption_UserId}]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT
ValidUserOption."UserId" AS ValidUserOption_UserId,
ValidUserOption."OptionKey" AS ValidUserOption_OptionKey,
ValidUserOption."OptionValue" AS ValidUserOption_OptionValue,
ValidUserOption."AccessCode" AS ValidUserOption_AccessCode,
ValidUserOption."StampDate" AS ValidUserOption_StampDate,
ValidUserOption."StampUser" AS ValidUserOption_StampUser,
ValidUserOption."Comments" AS ValidUserOption_Comments
FROM
ValidUserOption Where ValidUserOption."UserId" = $P{userId}]]>
</queryString>
<field name="ValidUserOption_UserId" class="java.lang.String"/>
<field name="ValidUserOption_OptionKey" class="java.lang.String"/>
<field name="ValidUserOption_OptionValue" class="java.lang.String"/>
<field name="ValidUserOption_AccessCode" class="java.lang.Integer"/>
<field name="ValidUserOption_StampDate" class="java.sql.Timestamp"/>
<field name="ValidUserOption_StampUser" class="java.lang.String"/>
<field name="ValidUserOption_Comments" class="java.lang.String"/>
<variable name="ValidUserOption_UserId_1" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[$F{ValidUserOption_UserId}]]></variableExpression>
</variable>
<variable name="ValidUserOption_OptionKey_1" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[$F{ValidUserOption_OptionKey}]]></variableExpression>
</variable>
<variable name="ValidUserOption_OptionValue_1" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[$F{ValidUserOption_OptionValue}]]></variableExpression>
</variable>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="56" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="79" height="20"/>
<textElement>
<font fontName="Arial Black"/>
</textElement>
<text><![CDATA[ValidUserOption_UserId]]></text>
</staticText>
<staticText>
<reportElement x="79" y="0" width="79" height="20"/>
<textElement>
<font fontName="Arial Black"/>
</textElement>
<text><![CDATA[ValidUserOption_OptionKey]]></text>
</staticText>
<staticText>
<reportElement x="158" y="0" width="79" height="20"/>
<textElement>
<font fontName="Arial Black"/>
</textElement>
<text><![CDATA[ValidUserOption_OptionValue]]></text>
</staticText>
<staticText>
<reportElement x="237" y="0" width="79" height="20"/>
<textElement>
<font fontName="Arial Black"/>
</textElement>
<text><![CDATA[ValidUserOption_AccessCode]]></text>
</staticText>
<staticText>
<reportElement x="316" y="0" width="79" height="20"/>
<textElement>
<font fontName="Arial Black"/>
</textElement>
<text><![CDATA[ValidUserOption_StampDate]]></text>
</staticText>
<staticText>
<reportElement x="395" y="0" width="79" height="20"/>
<textElement>
<font fontName="Arial Black"/>
</textElement>
<text><![CDATA[ValidUserOption_StampUser]]></text>
</staticText>
<staticText>
<reportElement x="474" y="0" width="79" height="20"/>
<textElement>
<font fontName="Arial Black"/>
</textElement>
<text><![CDATA[ValidUserOption_Comments]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="79" height="20"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression><![CDATA[$F{ValidUserOption_UserId}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="79" y="0" width="79" height="20"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression><![CDATA[$F{ValidUserOption_OptionKey}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="158" y="0" width="79" height="20"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression><![CDATA[$F{ValidUserOption_OptionValue}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="237" y="0" width="79" height="20"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression><![CDATA[$F{ValidUserOption_AccessCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="316" y="0" width="79" height="20"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression><![CDATA[$F{ValidUserOption_StampDate}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="395" y="0" width="79" height="20"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression><![CDATA[$F{ValidUserOption_StampUser}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="474" y="0" width="79" height="20"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression><![CDATA[$F{ValidUserOption_Comments}]]></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>

No comments:

Post a Comment