Getting Started with Report Engine API development-data set with a parametric program

Source: Internet
Author: User
Tags stmt

Today we talk about the development of the report, the last week to open this series of introductory article is also three or four days, a poor view of the small series is sad ah, I hope you support me a lot! First, the problem description

in the actual application, it may be necessary to dynamically change the data source according to the table name, such as in the program data set, through the table name parameters passed in to the database to fetch the corresponding table as the data source. Because FineReport reads a data source by abstracttabledata an abstract class, all of the above data sources inherit their abstract methods , so as long as the user implements the abstracttabledata abstract class, it is possible to use a custom type of data source ( program DataSet ) , theFineReport Report Engine is able to read the defined data source for use as a report data source. An example of this is illustrated below.

Second, the realizationOriginalManagement

same as the simple program data set, which is inherited Abstracttabledata . You can refer to my last article.

Third, the realization step

3.1 Defining Parameters
Define a parameter and define the data table structure, with the following code:

    Public Paramtabledatademo () {              //define TableName parameter              this.parameters = new parameter[] {new Parameter ("TableName")};              //Define program Data set column name              columnnames = new String[columnnum];              for (int i = 0; i < Columnnum; i++) {                  columnnames[i] = "column#" + string.valueof (i);              }          }  

3.2   setting up data
Put the data into the defined table with the following code:
public void init () {//ensures only one execution once if (valueList! = null) {return;        }//Save the resulting database table name String tableName = Parameters[0].getvalue (). toString ();        Constructs the SQL statement and prints out the String sql = "SELECT * from" + TableName + ";";        Frcontext.getlogger (). info ("Query SQL of Paramtabledatademo: \ n" + SQL);        Saved result set valueList = new ArrayList ();        The following begins to establish a database connection, according to the SQL statement just query Connection conn = This.getconnection ();            try {Statement stmt = conn.createstatement ();            ResultSet rs = stmt.executequery (SQL);            Get the details of the record, and then get the total number of columns ResultSetMetaData RSMD = Rs.getmetadata ();            Colnum = Rsmd.getcolumncount ();            Save data with Object object[] Objarray = null;                while (Rs.next ()) {Objarray = new Object[colnum];                for (int i = 0; i < Colnum; i++) {Objarray[i] = Rs.getobject (i + 1);              }  Add this line of data to ValueList Valuelist.add (Objarray);            }//Release database resource Rs.close ();            Stmt.close ();            Conn.close (); Print the total number of data rows taken Frcontext.getlogger (). info ("Query SQL of Paramtabledatademo: \ n" + Valuelist.s    Ize () + "rows selected");    } catch (Exception e) {e.printstacktrace (); }}

3.3 Complete Data Set code

The code for the complete data set of the parameter program is as follows:

  Package com.fr.data;    Import java.sql.Connection;    Import Java.sql.DriverManager;    Import Java.sql.ResultSet;    Import Java.sql.ResultSetMetaData;    Import java.sql.Statement;    Import java.util.ArrayList;    Import Com.fr.base.FRContext;    Import Com.fr.data.AbstractTableData;    Import Com.fr.base.Parameter;     public class Paramtabledatademo extends Abstracttabledata {//Column an array group, save program dataset all column names private string[] columnnames = null;    Defines the number of columns in the program dataset private int columnnum = 10;    Save the actual number of columns in the query table private int colnum = 0;    Save query Gets column value private ArrayList valueList = null; constructor that defines the table structure, which has 10 columns of data, named Column#0,column#1, ... Column#9 public Paramtabledatademo () {//define tablename parameter this.parameters = new parameter[] {new Parameter        ("TableName")};        Define program DataSet column name ColumnNames = new String[columnnum];        for (int i = 0; i < Columnnum; i++) {columnnames[i] = "column#" + string.valueof (i); }}//Implement the other four methods public int getColumnCount () {return columnnum;    } public String getcolumnname (int columnindex) {return columnnames[columnindex];        } public int GetRowCount () {init ();    return Valuelist.size ();        } public Object getvalueat (int rowIndex, int columnindex) {init ();        if (columnindex >= colnum) {return null;    } return ((object[]) Valuelist.get (RowIndex)) [ColumnIndex];        }//Prepare data public void init () {//Ensure only once if (valueList! = null) {return;        }//Save the resulting database table name String tableName = Parameters[0].getvalue (). toString ();        Constructs the SQL statement and prints out the String sql = "SELECT * from" + TableName + ";";        Frcontext.getlogger (). info ("Query SQL of Paramtabledatademo: \ n" + SQL);        Saved result set valueList = new ArrayList ();        The following begins to establish a database connection, according to the SQL statement just query Connection conn = This.getconnection (); try {Statement stmt = conn.createstAtement ();            ResultSet rs = stmt.executequery (SQL);            Get the details of the record, and then get the total number of columns ResultSetMetaData RSMD = Rs.getmetadata ();            Colnum = Rsmd.getcolumncount ();            Save data with Object object[] Objarray = null;                while (Rs.next ()) {Objarray = new Object[colnum];                for (int i = 0; i < Colnum; i++) {Objarray[i] = Rs.getobject (i + 1);            }//Add this line of data Valuelist.add (Objarray) in valueList;            }//Release database resource Rs.close ();            Stmt.close ();            Conn.close (); Print the total number of data rows taken Frcontext.getlogger (). info ("Query SQL of Paramtabledatademo: \ n" + Valuelis        T.size () + "rows selected");        } catch (Exception e) {e.printstacktrace (); }}//Get database connection drivername and URL can be replaced with the public Connection getconnection () {String drivername = "sun.jdbc) you need. Odbc. JdbcodBcdriver ";        String url = "Jdbc:odbc:driver={microsoft Access Driver (*.mdb)};D Bq=d:\\finereport_7.0\\webreport\\frdemo.mdb";        String username = "";        String password = "";        Connection con = null;            try {class.forname (drivername);        con = drivermanager.getconnection (URL, username, password);            } catch (Exception e) {e.printstacktrace ();        return null;    } return con;        }//Release some resources, because there may be repeated calls, so you need to release valuelist, releasing the results of the last query from public void release () throws Exception {super.release ();    This.valuelist = null; }}

compiling Paramtabledatademo.java , the generated Paramtabledatademo.class class file copy to report project /web-inf/classes directory. Since the class is in the com.fr.data Package, the class should eventually be placed under /web-inf/classes/com/fr/data . The program data source is now defined.

3.4 Configuring the program Data set

Create a new report, create a new program data source in the report dataset, select a program dataset that we have defined, such as a name that can be customized, such as divtable


3.5 Working with program data sets
After you have configured the program data source, you can use the defined program data set, select the DataSet, click the Preview button, you can enter the table name to dynamically get the corresponding data table, and create a template, such as



Note: If the data is not previewed, verify that the database connection is defined in the code snippet URL the address is correct.

can see that we've put Stscore the data in the table is extracted into the program dataset table, and as with other types of datasets, the cell data column bindings can be implemented by means of a drag-and-drop method.

All right, that's it for today! Today's procedures a little more, to seek attention, to recommend, for collection.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting Started with Report Engine API development-data set with a parametric program

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.