Developers use jasperreport-table data sources of different data sources

Source: Internet
Author: User

Preface

In the previous articles, we have explained some common reports in simple format, but sometimes our reports must be in the form of tables. In this case, we need to use our jrtablemodeldatasource.

 

Question

As before, the following steps are required to generate a report:

1.IntroductionJarFor more information, see《Static text report.


2.Create a report template:

This is the same as the aircraftreport report template in the previous article. For details, see map data sources for different data sources.

 

3.For details about how to compile a report template, see static text report..


4.WriteServlet:

 

Package COM. dan. jasper; import Java. AWT. borderlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. util. hashmap; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jtable; import javax. swing. table. defaulttablemodel; import net. SF. jasperreports. engine. jrexception; import net. SF. jasperreports. engine. jasperfillmanager; import net. SF. jasperreports. engine. jasperprint; import net. SF. jasperreports. engine. data. jrtablemodeldatasource; import net. SF. jasperreports. view. jasperviewer; public class tablemodelreport {jframe mainframe; borderlayout; defaulttablemodel tablemodel; jtable table = new jtable (); jbutton generatereportbutton = new jbutton ("Generate Report"); Public tablemodelreport () {mainframe = new jframe ("aircraft data"); borderlayout = new borderlayout (); generatereportbutton. addactionlistener (New reportgenerator (); populatetablemodel (); mainframe. setsize (640,150); mainframe. setvisible (true); mainframe. getcontentpane (). setlayout (borderlayout); mainframe. add (New jlabel ("aircraft data"), borderlayout. north); table. setmodel (tablemodel); mainframe. getcontentpane (). add (table, borderlayout. center); mainframe. getcontentpane (). add (generatereportbutton, borderlayout. south); mainframe. setvisible (true);} private void populatetablemodel () {string [] columnnames = {"ID", "name", "sex "}; string [] [] DATA = {"1", "rebecca1", "female" },{ "2", "rebeca2", "female "}, {"3", "rebeca3", "female" },{ "4", "rebecca4", "female" }}; tablemodel = new defaulttablemodel (data, columnnames );} private void displayreport () {jasperprint = generatereport (); jasperviewer = new jasperviewer (jasperprint); jasperviewer. setvisible (true);} private jasperprint generatereport () {jasperprint = NULL; try {jasperprint = jasperfillmanager. fillreport ("E:/project/work1/report3/webroot/WEB-INF/classes/reports/aircraftreport. jasper ", new hashmap (), new jrtablemodeldatasource (tablemodel);} catch (jrexception e) {e. printstacktrace ();} return jasperprint;} private class reportgenerator implements actionlistener {public void actionreceivmed (actionevent e) {displayreport () ;}} public static void main (string [] ARGs) {New tablemodelreport ();}}

 


5.ConfigurationWeb. xmlIs the configurationServlet

 

6.Run the project

Let's take a look at my running results:



Summary:

This table-based report is similar to the previous preview report, but it shows the table style and needs to preview and print the report function.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.