The report is very important to a program. It will help view the data from MySQL database or any database you are using in a paper format.
When using this feature, you can view and generate the report into pdf, word, and excel file format. This means that you cannot depend on the data stored in your database because by using this feature, you can generate and print the data directly to a paper and become your hard copy data.In this tutorial, I am using Jasper reports together with my Netbeans IDE so that this feature works properly. You can download the standalone jasper reports and install it to your computer or you can add it as plugins from your Netbeans using your downloaded plugins with nbm format.
Before anything else, make sure that you already created your report using your Jasper Reports plugins or installer. This tutorial shows only the code on how to display a report using Jasper in Java. When generating your report, it will create a file with .jasper file format. We use that format in viewing the report. If you need to change the report, you can edit the .jrxml file and complied again to generate or create a new .jasper file.
Code for accessing Jasper Libraries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.sf.jasperreports.engine.JRException; | |
import net.sf.jasperreports.engine.JasperFillManager; | |
import net.sf.jasperreports.engine.JasperPrint; | |
import net.sf.jasperreports.swing.JRViewer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String reportName = "yourproject/report1.jasper"; | |
InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName); | |
HashMap hm = new HashMap(); | |
hm.put("lastname", city.getSelectedItem().toString()); | |
JasperPrint jp; | |
try { | |
jp = JasperFillManager.fillReport(is, hm, conn); | |
JRViewer jv = new JRViewer(jp); | |
JInternalFrame bl = new JInternalFrame(); | |
JDesktopPane desktopPane = getDesktopPane(); | |
desktopPane.add(bl); | |
bl.getContentPane().add(jv); | |
bl.setVisible(true); | |
bl.setSize(new Dimension(1024,600)); | |
bl.setClosable(true); | |
bl.setTitle("Report Viewer"); | |
} catch (JRException ex) { | |
Logger.getLogger(Reports.class.getName()).log(Level.SEVERE, null, ex); | |
} |
If this tutorial is helpful to you, please don't forget to subscriber to our newsletter or share these topics with any social media platform below. You can also submit your comment below.
0 Comments