Developers use jasperreport to generate reports from data sources

Source: Internet
Author: User

Preface

The first two articles describe how to query databases, but they all interact with databases through Report Templates. That is to say, our query statements and parameter definitions are all written in the templates, so sometimes it is not convenient, so today I will introduce you to interact with the database through servlet, And the template is only responsible for display.


Question

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

1. Introduce the jar package. See static text report.


2. Create a report template:

Because servlet is used to interact with the database this time, the query statement is not in our report:


<? Xmlversion = "1.0" encoding = "UTF-8"?>

<Jasperreportxmlns = "http://jasperreports.sourceforge.net/jasperreports"

Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

Xsi: schemalocation = "http://jasperreports.sourceforge.net/jasperreportshttp://jasperreports.sourceforge.net/xsd/jasperreport.xsd"

Name = "jrresultreport">

<Fieldname = "ID" class = "Java. Lang. Integer"/>

<Fieldname = "name" class = "Java. Lang. String"/>

<Fieldname = "sex" class = "Java. Lang. String"/>

<Pageheader>

<Bandheight = "30">

<Statictext>

<Reportelementx = "0" Y = "0" width = "69" Height = "24"/>

<Textelementverticalalignment = "bottom"/>

<Text> id </text>

</Statictext>

<Statictext>

<Reportelementx = "140" Y = "0" width = "79" Height = "24"/>

<Text> name </text>

</Statictext>

<Statictext>

<Reportelementx = "280" Y = "0" width = "69" Height = "24"/>

<Text> sex </text>

</Statictext>

</Band>

</Pageheader>

<Detail>

<Bandheight = "30">

<Textfield>

<Reportelementx = "0" Y = "0" width = "69" Height = "24"/>

<Textfieldexpressionclass = "Java. Lang. Integer">

$ F {ID}

</Textfieldexpression>

</Textfield>

<Textfield>

<Reportelementx = "140" Y = "0" width = "69" Height = "24"/>

<Textfieldexpressionclass = "Java. Lang. String">

$ F {name}

</Textfieldexpression>

</Textfield>

<Textfield>

<Reportelementx = "280" Y = "0" width = "69" Height = "24"/>

<Textfieldexpressionclass = "Java. Lang. String">

$ F {sex}

</Textfieldexpression>

</Textfield>

</Band>

</Detail>

</Jasperreport>

3. compile the report template. See static text report.


4. Compile servlet:

In this example, the servlet interacts with the database (jrresultsetdatasource Data Source). Therefore, the servlet establishes a connection to the database and a query statement:

Packagecom. Dan. servlet;

 

Importjava. Io. ioexception;

Importjava. Io. inputstream;

Importjava. Io. printwriter;

Importjava. Io. stringwriter;

Importjava. SQL. connection;

Importjava. SQL. drivermanager;

Importjava. SQL. resultset;

Importjava. SQL. statement;

Importjava. util. hashmap;

 

Importjavax. servlet. servletexception;

Importjavax. servlet. servletoutputstream;

Importjavax. servlet. http. httpservlet;

Importjavax. servlet. http. httpservletrequest;

Importjavax. servlet. http. httpservletresponse;

 

Importnet. SF. jasperreports. Engine. jrresultsetdatasource;

Importnet. SF. jasperreports. Engine. jasperrunmanager;

 

/**

* Fill in with jrresultsetdatasource

* @ Author zdd

*

*/

Publicclass dbreportdsservlet extends httpservlet {

 

@ Override

Protectedvoid doget (httpservletrequest req, httpservletresponse resp)

Throwsservletexception, ioexception {

Connectionconnection;

Statementstatement;

Resultsetresultset;

 

Resp. setcontenttype ("application/pdf ");

 

Servletoutputstreamservletoutputstream = resp. getoutputstream ();

 

Inputstreamreportstream = getservletconfig (). getservletcontext (). getresourceasstream ("/WEB-INF/classes/reports/jrresultreport. Jasper ");

 

Stringquery = "select T. ID, T. Name, T. Sex from t_user t ";

 

Try {

Class. forname ("oracle. JDBC. Driver. oracledriver ");

Connection = drivermanager. getconnection ("JDBC: oracle: thin: @ 192.168.24.36: 1521: testreport", "test", "test ");

Statement = connection. createstatement ();

Resultset = statement.exe cutequery (query );

Jrresultsetdatasourceresultsetdatasource = new jrresultsetdatasource (resultset );

Jasperrunmanager. runreporttopdfstream (reportstream, servletoutputstream, newhashmap (), resultsetdatasource );

Resultset. Close ();

Statement. Close ();

Connection. Close ();

Servletoutputstream. Flush ();

Servletoutputstream. Close ();

} Catch (effectione ){

Stringwriterstringwriter = new stringwriter ();

Printwriterprintwriter = new printwriter (stringwriter );

E. printstacktrace (printwriter );

Resp. setcontenttype ("text/plain ");

Resp. getoutputstream (). Print (stringwriter. tostring ());

}

}

 

}

5. configuring web. xml means configuring servlet.


6. Run the project

Let's take a look at my running results:

 

 

Summary:

In fact, so many instances are very simple. The difference between each instance is that they are used in different ways. Other instances are basically similar.

 

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.