RSS

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.

 
Leave a comment

Posted by on October 20, 2012 in Java

 

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Life Lessons and morals: 5 stories from the Yoruba language and culture

Life Lessons and morals: 5 stories from the Yoruba language and culture.

 
Leave a comment

Posted by on September 21, 2012 in Life Lessons

 

Tags: , , , , , , , , , , , , , , , , ,

Seven Lessons from the Land of Oz

Seven Lessons from the Land of Oz.

 
Leave a comment

Posted by on September 2, 2012 in Office

 

Tags: , , , , , ,

Tribute to My sister at eighteen

Reblogged from adebayoadegbite:

Happy birthday to you (2x)

Happy Birthday to you Deola

HappyBirthday to you

We wish you many happy returns of the day

Long life and Prosperity

HIP! HIP!! HOORAAAY!!!

                 It is appropriate that this piece commemorating your eighteenth birthday starts with the Birthday song popularly known and sung all over the world. Well first of all I have to beg your apologies that I did not write this earlier, well I’m really sorry but then since you are eighteen until next year , I believe I’m still in order.

Read more… 2,265 more words

All ladies and maybe guys too that will soon clock 18.
 
Leave a comment

Posted by on August 1, 2012 in Uncategorized

 

Installing and Deploying a war file in tomcat

1. Download apache-tomcat-7.0.28 or the latest version from :

http://tomcat.apache.org/download

2. Place the downloaded zip file in any location and uzip it, in my case I placed it in the location shown below.
C:\Utilities\apache-tomcat-7.0.28
3. Navigate to the bin folder as shown below
C:\Utilities\apache-tomcat-7.0.28\bin
4. Launch the startup.bat file (for windows) and you should get a screen as shown below:

Tomcat startup screen

5. From the browser, input the url:
localhost:8080

A screen like this should appear:

Tomcat Manager
6. To deploy a .war file, click the Manager App button as shown by the arrow above but you would need to set up a username and password for the manager.
7a. To set the username and password for the manager, navigate to tomcat-users file as shown in the link below:
C:\Utilities\apache-tomcat-7.0.28\conf\tomcat-users

b.Paste the line below where you have a commented list of user tags. You can also uncomment the tags:
<role rolename=”manager-gui”/>
<user username=”tomcat” password=”s3cret” roles=”manager-gui”/>
Here is a full content of the role tags aspect of tomcat-users.xml
<role rolename=”tomcat”/>
<role rolename=”role1″/>
<user username=”tomcat” password=”tomcat” roles=”tomcat”/>
<user username=”both” password=”tomcat” roles=”tomcat,role1″/>
<user username=”role1″ password=”tomcat” roles=”role1″/>
<role rolename=”manager-gui”/>
<user username=”tomcat” password=”s3cret” roles=”manager-gui”/>

8. Restart Tomcat and input the link below in the browser:
localhost:8080

9. Click on the ‘Choose file’ Button This would manage the upload of your war file

Deploying war file
10. To deploy a war file, click the Choose file button as shown above. This will prompt you to upload the war file you want to deploy.

11. Click the deploy button
Your war file will be deployed and can be referenced from the browser using:
localhost:8080/warfilename

Deployment

Your comments and observations will be highly appreciated.

 

 
1 Comment

Posted by on June 26, 2012 in Java

 

Tags: , , , , , , , , , , , ,

Spring Error

In an event you encounter an error as below in the development of a spring application with STS; you can find the solution as stated.

Error:

Exception in thread “main” org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 14 in XML document from class path resource [META-INF/spring/app-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The prefix “p” for attribute “p:messageProvider-ref” associated with an element type “bean” is not bound.
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions
(XmlBeanDefinitionReader.java:396)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions
(XmlBeanDefinitionReader.java:334)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions
(XmlBeanDefinitionReader.java:302)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:143)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:178)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:149)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:212)
org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions
(AbstractXmlApplicationContext.java:126)
org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions
(AbstractXmlApplicationContext.ja va:92)
org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory
(AbstractRefreshableApplicationContext.java:130)
org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory
(AbstractApplicationContext.java:467)
org.springframework.context.support.AbstractApplicationContext.refresh
(AbstractApplicationContext.java:397)
org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
com.apress.prospring3.ch2.HelloWorldSpringDI.main(HelloWorldSpringDI.java:15)
Caused by: org.xml.sax.SAXParseException: The prefix “p” for attribute “p:messageProvider-ref” associated with an element type “bean” is not bound.
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError
(ErrorHandlerWrapper.java:174)
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError
(XMLErrorReporter.java:388)
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError
(XMLErrorReporter.java:318)
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement
(XMLNSDocumentScannerImpl.java:334)
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next
(XMLDocumentFragmentScannerImpl.java:2755)
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next
(XMLDocumentScannerImpl.java:648)
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next
(XMLNSDocumentScannerImpl.java:140)
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument
(XMLDocumentFragmentScannerImpl.java:511)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
… 14 more

Solution:
To resolve this problem, the line below would have to be included in the xml schema definition in the first few lines of your xml file (in my case app-context.xml)
xmlns:p=”http://www.springframework.org/schema/p&#8221;

so i have the first 5 lines of my xml file to be this:
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns:p=”http://www.springframework.org/schema/p&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;

*This works fine.

 
Leave a comment

Posted by on June 24, 2012 in Java, Office

 

Tags: , , , , , , , , , , , , , , , , , ,

 
Follow

Get every new post delivered to your Inbox.