Jasper Reports with T24 Jbase

Hello all,

Today we shall be looking at how to generate a simple pdf report from T24 on Jbase with Jasper.  This report retrieves the Mnemonic, Sector and Industry from the Customer table.

Although it is required to have a knowledge of Java to be able to follow this tutorial, contributions and questions would be highly appreciated.

You as a reader might decide to achieve this your own way and this would mean that you do not have to follow this tutorial if you have a better way 🙂

Lets get started.

Step 1: Create a java class that connects to jbase and performs all the other magic acts

This can be achieved by using any favourite IDE of yours and this tutorial will not be based on any particular IDE. The code snippet of the main class is pasted below.

==============================

package newjsp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
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.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class Main {

Connection conn;

public void generateReport() {

try {
Class.forName(“com.jbase.jdbc.driver.JBaseJDBCDriver”);
//This is where you specify the credentials of the database you are connecting to.
conn = DriverManager.getConnection(“jdbc:jbase:thin:@localhost:9494:R09″,”R09″,”123456”);
System.out.println(“Connected…”);
System.out.println(“Loading Report Designs”);
InputStream input = new FileInputStream(new File(“resources/report3.jrxml”));
JasperDesign jasperDesign = JRXmlLoader.load(input);

System.out.println(“Compiling Report Designs”);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

System.out.println(“Creating JasperPrint Object”);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(“ReportTitle”, “PDF JasperReport”);

System.out.println(“Filling Report to File”);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
//System.out.println(“Sector is “+ conn );
//Exporting the report
OutputStream output = new FileOutputStream(new File(“resources/customer.pdf”));
JasperExportManager.exportReportToPdfStream(jasperPrint, output);

System.out.println(“Report Generation Complete”);
conn.close();
} catch (FileNotFoundException e) {
} catch (JRException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
}
}

public static void main(String[] args) {
new Main().generateReport();
}
}
=====================================
Before moving on there are some things that you need to do so as to be able to run the code in step 1 above successfully

1. If you are making use of some extra classes, make sure they have been placed in the right packages in the project and they are error free.
2. You may need to add some jars to your project and one of them is the jdbc jar which should come with your jbase package.

3.Make sure the agent in the database environment specified is running and this can be done using the command “jbase_agent p- <port number> ”

4.You can place your questions if you are still having any other problem apart from the ones mentioned above.

Step 2: Create an xml file to be used for the formatting and retrieval of the needed information

This xml file may be created manually or created using tools like Ireport and some others. The fact is just that you should know what you want.

After creating the xml file, save it as a “.jrxml” file and you do not need to compile it to a “.jasper” file

Then specify the link to where you have placed the xml file in your java code. as in step 1.

The xml file used for this tutorial is pasted below.

==================

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE jasperReport PUBLIC “//JasperReports//DTD Report Design//EN”
http://jasperreports.sourceforge.net/dtds/jasperreport.dtd”&gt;
<jasperReport name=”CustomerReport_TeejayT24″>
<queryString>
<![CDATA[SELECT * FROM FBNK.CUSTOMER ]]>
</queryString>
<field name=”MNEMONIC” />
<field name=”SECTOR” />
<field name=”INDUSTRY” />
<title>
<band height=”50″>
<staticText>
<reportElement x=”0″ y=”0″ width=”180″ height=”15″/>
<textElement/>
<text><![CDATA[Jasper Report – Teejay]]></text>
</staticText>
<staticText>
<reportElement x=”0″ y=”30″ width=”180″ height=”15″/>
<textElement/>
<text><![CDATA[Mnemonic]]></text>
</staticText>
<staticText>
<reportElement x=”140″ y=”30″ width=”180″ height=”15″/>
<textElement/>
<text><![CDATA[SECTOR]]></text>
</staticText>
<staticText>
<reportElement x=”200″ y=”30″ width=”180″ height=”15″/>
<textElement/>
<text><![CDATA[INDUSTRY]]></text>
</staticText>
</band>
</title>
<detail>
<band height=”30″>
<textField>
<reportElement x=”0″ y=”0″ width=”69″ height=”24″ />
<textFieldExpression>
<![CDATA[$F{MNEMONIC}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x=”140″ y=”0″ width=”69″ height=”24″ />
<textFieldExpression>
<![CDATA[$F{SECTOR}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x=”200″ y=”0″ width=”69″ height=”24″ />
<textFieldExpression>
<![CDATA[$F{INDUSTRY}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

================================

Step 3: Make sure all your codes have been compiled and then run it.

A pdf report will be produced in the location you have specified in your program with the retrieved report. A sample of the generated report is as shown below.

Sample pdf report generated

That is all about this tutorial.

Please note that you can also generate any other type of report as supported by JasperReports, this tutorial is based mainly on pdf report generation.

Thanks for following the tutorial and i would also like to acknowledge the works of Suhas on jasper reports

Thanks and Regards.

Published by adetunjiadegbite

Java Developer

Leave a comment