Developers use jasperreport to generate reports from data sources

Source: Internet
Author: User


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.


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 = ""

Xmlns: xsi = ""

Xsi: schemalocation = ""

Name = "jrresultreport">

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

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

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


<Bandheight = "30">


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

<Textelementverticalalignment = "bottom"/>

<Text> id </text>



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

<Text> name </text>



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

<Text> sex </text>





<Bandheight = "30">


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

<Textfieldexpressionclass = "Java. Lang. Integer">

$ F {ID}




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

<Textfieldexpressionclass = "Java. Lang. String">

$ F {name}




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

<Textfieldexpressionclass = "Java. Lang. String">

$ F {sex}






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 {





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: @ 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:




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: 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.