ireport connect MySQL to create a chart report

Source: Internet
Author: User

List the resources you need:

1, MySQL database installed

2, Ireport+jasperreport configuration good

3, I use the Myeclipse,mysql drive Jar pack Don't forget

Part I: Creating a database connection

Package Com.mysqlsource;import Java.sql.connection;public class Database {private String Dburl =  "jdbc:mysql:// Localhost:3306/bookdb ";  Private String dbuser= "root";  Private String dbpwd= "123456";  Public Database () throws exception{     class.forname ("Com.mysql.jdbc.Driver");  }  Public Connection getconnection () throws exception{      return java.sql.DriverManager.getConnection (Dburl,dbuser, DBPWD);  }  public void CloseConnection (Connection con) {    try{        if (con!=null) con.close ();      } catch (Exception e) {        e.printstacktrace ();}}  }
This format is basically the same, where Bookdb is the name of my new database, getconnection and CloseConnection are two operating switches, it is good to directly create a database object call when using.

Part II: First Look at my Database books table

Open ireport Create a new table do not understand the words to see other people's blog, a lot of. Find the data source on the interface open to make the following selections

This should be very simple, after setting up Click Test will prompt the test success, or you are some of the settings are not done, and then re-check again

Open the palette find drag to any bands, in iReport4.6.0 there is a part of the table is thought, part of no, this does not matter all the way next down

If you do not, right-click on the chart and select chart data.

Here is the default name, double-click the default name to open the property settings screen

About the meaning and function of the various fields, I have written before the article, here is not to repeat, if necessary, turn over the front of the good.

When set up, click Preview to see the following

The reason is that on the right side of the chart in the design panel, there is an attribute evaluation time, roughly meaning when to update the value, it defaults to now

In this case, only you put in detail bands will appear, but it will appear many times, not what we want. Setting it to report is updated after the reporting data is configured and previewed again

Part Three: After doing this, it is enough for the people who can connect to the database, but if you want to dynamically generate the report that the customer wants through the web, then we still have to connect to the database through the network, then dynamic the template we need

I use Myeclipse+tomcat to make a website

Package Com.mysqlsource;import Java.io.ioexception;import Java.io.outputstream;import java.net.URLEncoder;import Java.sql.connection;import Javax.servlet.servletexception;import Javax.servlet.http.*;import Net.sf.jasperreports.engine.jrexporterparameter;import Net.sf.jasperreports.engine.jasperfillmanager;import Net.sf.jasperreports.engine.jasperprint;import Net.sf.jasperreports.engine.jasperreport;import Net.sf.jasperreports.engine.export.jrpdfexporter;import Net.sf.jasperreports.engine.util.jrloader;public Class Mysqlsource extends Httpservlet{public void doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {//TODO auto-generated method Stubdopost (req, resp);} public void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {//TODO auto-g Enerated method Stubtry{string Root_path=this.getservletcontext (). Getrealpath ("/"); Root_path=root_path.replace ("\ \", "/"); String file_path=root_path+ "Chart/test_char.jasPer ";D atabase data=new Database (); Connection con=data.getconnection (); Jasperreport report= (Jasperreport) jrloader.loadobject (File_path);  Jasperprint print=jasperfillmanager.fillreport (report, NULL, con);d ata.closeconnection (con);          OutputStream Ouputstream = Resp.getoutputstream ();        Resp.setcontenttype ("Application/pdf");          Resp.setcharacterencoding ("UTF-8"); Resp.setheader ("Content-disposition", "attachment;                      Filename=\ "" + Urlencoder.encode ("PDF Report", "UTF-8") + ". pdf\" ");          Export PDF Jrpdfexporter exporter = new Jrpdfexporter () using the Jrpdfexproter exporter;        Exporter.setparameter (Jrexporterparameter.jasper_print, PRINT);          Exporter.setparameter (Jrexporterparameter.output_stream, Ouputstream);                        Exporter.exportreport ();  Ouputstream.close (); }catch (Exception e) {e.printstacktrace ();}}}
File list

Test_char.jasper (originally wanted to be test_chart.jasper later found that when the creation time less hit a letter) is ireport preview compiled after the build, found in your creation directory

The code is simple, with several key functions:

Jasperreport report= (Jasperreport) jrloader.loadobject (File_path); Jasperprint print=jasperfillmanager.fillreport (report, NULL, con), <pre name= "code" class= "HTML" > Jrpdfexporter Exporter = new Jrpdfexporter ();  Exporter.setparameter (Jrexporterparameter.jasper_print, PRINT); Exporter.setparameter (jrexporterparameter.output _stream, Ouputstream);  
These functions have also been mentioned in previous articles, so they are no longer verbose.
Special Note: To put jasperreport Lib files are best placed in the Web project Web-inf/lib directory, raw error
If you need source code, leave the mailbox, hope to discuss together










ireport connect MySQL to create a chart report

Related Article

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.