JasperReports report Data Sources-jdbc, JavaBeans, TableModel, XML, Csv__java

Source: Internet
Author: User
Tags pack

1.JDBC Data Source

This will create a simple database report using the JDBC data source, using the MySQL database.

/** * * @author Source God * Annotated: Omitted import Introduction Package/public class Jdbcdatasource {* * * get MySQL database connection * @return * /public static Connection getconnection () throws ClassNotFoundException, SQLException {String driver = "com. Mysql.jdbc.Driver "; Driver name String user = "root"; MySQL configuration user name String password = "Yuanshen"; Java connection mysql configuration password String url = "Jdbc:mysql://localhost:3306/jdbc"; The URL points to the database name to be accessed JDBC Class.forName (driver); Load driver Connection conn = drivermanager.getconnection (URL, user, password);
    Connect database return conn;
            public static void Main (string[] args) throws ClassNotFoundException, SQLException {try {
            (1) Load report Design template file Jasperdesign jasperdesign = jrxmlloader.load ("Jdbc.jrxml");
            (2) Compiling the report template Jasperreport Jasperreport = Jaspercompilemanager.compilereport (jasperdesign); (3) Fill report jasperprint Jasperprint = JaspeRfillmanager.fillreport (Jasperreport, New HashMap (), getconnection ());
            (4) Preview the report, the second parameter represents Isexitonclose, set to False jasperviewer JV = new Jasperviewer (jasperprint,false); Jv.pack ();
	    Set the optimal form size according to the control's preferred size perferredsize, so that the window shows all the controls Jv.settitle ("Print Preview");
        Jv.setvisible (TRUE);
        catch (Jrexception e) {e.printstacktrace (); }
    }
}

Getconnection () is a function that encapsulates a database operation that returns a connection type of database connection object that the report engine uses to base the SQL query statement in the Jrxml report template The template is populated by taking the corresponding data out of the database.

2.JavaBean Data Source

in JasperReports, two data source implementations are provided to support the JavaBeans object's data source.

Where net.sf.jasperreports.engine.data.JRBeanArrayDataSource can support the array form of JavaBeans,

The other one can support the implementation of the JavaBeans object data source as Net.sf.jasperreports.engine.data.JRBeanCollectionDataSource, that is, the implementation can support the JavaBeans of the collection form. The following are JavaBeans codes that individuals write to support the form of a collection:

/** * * @author Source God * Annotated: Omitted import Introduction Package/public class Javabeandatasource {* * * get JavaBean Data * @return
        * * public static list<user> GetData () {list<user> data = new arraylist<user> (); Data.add (New User ("1", "Yuanshen"));
    Suppose there are only two attributes in the user class, here id= "1", name= "Yuanshen" return data; public static void Main (string[] args) {try {//(1) Load report Design template file Jasperdesign ja
            Sperdesign = Jrxmlloader.load ("Javabeans.jrxml");
            (2) Compiling the report template Jasperreport Jasperreport = Jaspercompilemanager.compilereport (jasperdesign);
            (3) Fill report HashMap params = new HashMap ();
            
            Params.put ("title", "Data Source from JavaBeans"); Jasperprint jasperprint = Jasperfillmanager.fillreport (Jasperreport, params, new Jrbeancollectiondata

            Source (GetData ())); (4) Preview the report, the second parameter represents Isexitonclose, set to false here JaspErviewer JV = new Jasperviewer (jasperprint,false); Jv.pack ();
	    Set the optimal form size according to the control's preferred size perferredsize, so that the window shows all the controls Jv.settitle ("Print Preview");
        Jv.setvisible (TRUE);
        catch (Jrexception e) {e.printstacktrace (); }
    }
}

In JasperReports, parameters are organized through a map collection, and in the example program (3), a variable named title is passed to the report template with the value "Data Source from JavaBeans". After the collection of construction parameters is complete, the parameters can be passed to the report template through the Fillreport function, and the method of accepting and using parameters has been defined in the report template, and through these steps, the process of passing parameters from the program to the report template is completed.

3.TableModel Data Source

/** * * @author Source God * Annotated: Omitted import Introduction Package/public class Tablemodeldatasource {/* * generate, and preview report * @param Datam
     Odel * Call: Export (Table.getmodel ()); * * public void Export (TableModel datamodel) {try {//(1) Load report Design template file Jasperdesign jasp
            Erdesign = Jrxmlloader.load ("Tablemodel.jrxml");
            (2) Compiling the report template Jasperreport Jasperreport = Jaspercompilemanager.compilereport (jasperdesign);
            (3) Fill report HashMap params = new HashMap ();
            
	    Params.put ("title", "Data Source from TableModel"); Jasperprint jasperprint = Jasperfillmanager.fillreport (Jasperreport, params, new JRTABLEMODELDATASOURC
             E (Datamodel));
            (4) Preview the report, the second parameter represents Isexitonclose, set to False jasperviewer JV = new Jasperviewer (jasperprint,false); Jv.pack ();
	    Set the optimal form size according to the control's preferred size perferredsize, so that the window shows all the controls Jv.settitle ("Print Preview");
        Jv.setvisible (TRUE); catch (JREXception e) {e.printstacktrace (); }
    }
}
In JasperReports, parameters are organized through a map collection, and in the example program (3), a variable named title is passed to the report template with the value "Data Source from TableModel". After the collection of construction parameters is complete, the parameters can be passed to the report template through the Fillreport function, and the method of accepting and using parameters has been defined in the report template, and through these steps, the process of passing parameters from the program to the report template is completed.

4.XML Data Source

/** * * @author Source God * Annotated: Omitted import Introduction Package/public class XmlDataSource {public static void main (string[] args) {
            try {//(1) Load report Design template file Jasperdesign jasperdesign = jrxmlloader.load ("Xmldatasource.jrxml");
            (2) Compiling the report template Jasperreport Jasperreport = Jaspercompilemanager.compilereport (jasperdesign);
            (3) Fill report HashMap params = new HashMap ();
            Params.put ("title", "Data source from XML data source");
            
            Document document = Jrxmlutils.parse (Jrloader.getlocationinputstream ("Jobs.xml"));
            Params.put (Jrxpathqueryexecuterfactory.parameter_xml_data_document, DOCUMENT);
            
            Params.put (Jrxpathqueryexecuterfactory.xml_number_pattern, "#,# #0. #");
            Jasperprint jasperprint = Jasperfillmanager.fillreport (Jasperreport, params);
    (4) Preview the report, the second parameter represents Isexitonclose, set to False jasperviewer JV = new Jasperviewer (jasperprint,false);        Jv.pack ();
	    Set the optimal form size according to the control's preferred size perferredsize, so that the window shows all the controls Jv.settitle ("Print Preview");
        Jv.setvisible (TRUE);
        catch (Jrexception e) {e.printstacktrace (); }
    }
}
When using an XML document as a data source, the data source is not provided directly, but the XML document and related settings are passed to the report template in the parameters. where the Fillreport () method has only two parameters, the contents and settings of all XML document data sources are placed in the params parameter. It defines 3 parameters that need to be passed to the report template:

1. In jasperreports, parameters are organized through a map collection, and in the example program (3), a variable named title is passed to the report template with the value "data source from XML data source." This is the title string that is passed to the report template here.

2. The second parameter, Jrxpathqueryexecuterfactory.parameter_xml_data_document, is a built-in parameter in JasperReports that is used to pass an XML data source document to the report template. The value of this parameter is a document object that can be obtained from the XML file through the Jrxmlutils parse method.

3. The third parameter is also the built-in parameter Jrxpathqueryexecuterfactory.xml_number_pattern in JasperReports, which defines the format of the numeric node values in this XML document.

With these parameters, you can pass the XML data source to the report template to complete the fill of the report template.

5.CSV Data Source

/** * * @author Source God * Notes: Omitted import Introduction Package/public class CsvDataSource {* * * * to get data from CSV file * @return * * private static Jrcsvdatasource Getdatasource () throws Jrexception {Jrcsvdatasource ds = new Jrcsvdatasource (J
        Rloader.getlocationinputstream ("Csvdatasource.csv")); Ds.setusefirstrowasheader (TRUE); The first row in the CSV file is read directly as column names string[] columnName = new string[] {"id", "name"};
        Provides only the data content, does not provide the column name, sets the CSV each column name Ds.setcolumnnames (columnName);
        Ds.setrecorddelimiter ("\ r \ n"); Ds.setfielddelimiter ("-");
    The default column delimiter in the CSV file is "," where you can set the delimiter for the column to "-" return DS; public static void Main (string[] args) {try {//(1) Load report Design template file Jasperdesign Jas
            Perdesign = Jrxmlloader.load ("Csvdatasource.jrxml");
            (2) Compiling the report template Jasperreport Jasperreport = Jaspercompilemanager.compilereport (jasperdesign);
            (3) Fill report HashMap params = new HashMap (); Params.puT ("title", "Data source from CSV data source");
            Jasperprint jasperprint = Jasperfillmanager.fillreport (Jasperreport, params, Getdatasource ());
            (4) Preview the report, the second parameter represents Isexitonclose, set to False jasperviewer JV = new Jasperviewer (jasperprint,false); Jv.pack ();
	    Set the optimal form size according to the control's preferred size perferredsize, so that the window shows all the controls Jv.settitle ("Print Preview");
        Jv.setvisible (TRUE);
        catch (Jrexception e) {e.printstacktrace (); }
    }
}

        in JasperReports, parameters are organized through a map collection, and in the example program (3), you need to pass a variable named title to the report template with the value "Data Source from CSV Data source. After the collection of construction parameters is complete, the parameters can be passed to the report template through the Fillreport function, and the method of accepting and using parameters has been defined in the report template, and through these steps, the process of passing parameters from the program to the report template is completed.

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.