Ireport+jasperreports Report Development Summary One (STRUTS2)

Source: Internet
Author: User

Development environment:

Ireport4.0.1+sqlserver2000+struts2

Functional objectives:

By struts2 the data information into the report designed by ireport, the data source way: JavaBean

Specific implementation:

1, a new report template named: Testreport.jrxml, according to the wizard click on the next page, select the data source as shown in the following figure:

Here are a variety of data sources, often we use JDBC and JavaBean methods, because JDBC connection, is directly in the template to write query statements, its advantage is that the operation is simple, but in the project once the change is very troublesome one thing, and more rigid, business logic and the performance layer sticky more tightly, So, here I choose to connect in a JavaBean way to connect, the query business and report separated, in order to facilitate the maintenance and expansion of later functions. Here I create a new JavaBean connection mode data source.

2, because this is only a single report template, the data source does not exist in the field, in the subsequent wizard operation process directly point to the next step, until the new completion.

3, to build STRUTS2 project projects, this does not have to say that everyone will (the jar package that we need in our project, all introduced, note: you introduce the version of the jar package is best and ireport version of the same, responsible for May report an exception), I introduced the project in the jar version as shown in the following figure:

4. Create a database and table in Salserver as shown in the following illustration:

5, the early stage of the basic work completed now began to truly enter the Magical report tour:

First, create a class named User, which is actually a formbean, which is the set and get methods corresponding to our Userdemo table fields:

Package com.ansure.moudle; public class User {private int u_id; private string u_name, private string u_pwd; private string u_email; public user () { public User (int ID, string name, string pwd, string email) {this.u_id = id; this.u_name = name; this.u_pwd = pwd; . u_email = email; public int getu_id () {return u_id.} public void setu_id (int uId) {u_id = uId.} public String Getu_email () {return u _email; public void Setu_email (String email) {this.u_email = email;} Public String Getu_name () {return u_name;} public void Setu_name (String uName) {u_name = uName;} public String getu_pwd () {return u_pwd;} public void Setu_pwd (String upwd) { U_pwd = upwd; } }

Second, create database connection class Dbpool, here casually wrote a:

   package Com.ansure.utill; * * Data Access Component/import java.io.*; Import java.util.*; Import java.sql.*; /** * @ Author Haizi * @ creation Date 2011-04-12 * @ VERSION V 1.0/public class dbpool{private static Dbpool instance = NULL; public Stati C synchronized Connection getconnection () {if (instance = = null) {instance = new Dbpool ();} return Instance._getconnecti On (); Private Dbpool () {super ();} private Connection _getconnection () {try{String sdbdriver = "Com.microsoft.jdbc.sqlserver. SQLServerDriver "; String sconnection = "Jdbc:microsoft:sqlserver://localhost:1433;databasename=mydatabase"; String suser = "sa"; String Spassword = "sa"; Class.forName (Sdbdriver). newinstance (); Return drivermanager.getconnection (Sconnection,suser,spassword); catch (Exception se) {System.out.println (SE); return null;}} Free resources public static void Dbclose (Connection conn,preparedstatement ps,resultset rs) throws SQLException {rs.close (); Ps. Close (); Conn.close (); public static void Main (String arg[]) {new Dbpool(). _getconnection (); } }

   Third: Create a data query for the business class Userdao, only as a reference:

  package Com.ansure.dao; Import java.sql.Connection; Import Java.sql.ResultSet; Import java.sql.SQLException; Import java.sql.Statement; Import java.util.ArrayList; Import java.util.List; Import Com.ansure.moudle.User; Import Com.ansure.utill.dbPool; public class Userdao {public Userdao () {} Connection conn; Statement St; ResultSet rs; Public List getallstudent () {String sql= ""; String sqls= "Select SQLText from SQLText where id=1"; List userlist=new ArrayList (); try {conn=dbpool.getconnection (); St=conn.createstatement (); Rs=st.executequery (SQLS); while (Rs.next ()) {sql= Rs.getstring ("SQLText"). ToString (); } rs=null; SYSTEM.OUT.PRINTLN (SQL); Rs=st.executequery (SQL); while (Rs.next ()) {User User=new User (rs.getint ("u_id"), Rs.getstring ("U_name"), Rs.getstring ("U_pwd"), Rs.getstring ( "U_email")); Userlist.add (user); } catch (SQLException e) {try {conn.close ();} catch (SQLException E1) {e1.printstacktrace ();} e.printstacktrace (); return null; return userlist; }} &nbsP

  Fourth: Create action the specific method is as follows, note the Get and set methods for UserList and map:

  Package com.ansure.action; Import Java.io.File; Import java.util.ArrayList; Import Java.util.HashMap; Import java.util.List; Import Java.util.Map; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; Import Org.apache.struts2.ServletActionContext; Import Net.sf.jasperreports.engine.export.JRHtmlExporter; Import Net.sf.jasperreports.engine.export.JRHtmlExporterParameter; Import Com.ansure.dao.UserDao; Import Com.ansure.moudle.User; Import Com.opensymphony.xwork2.ActionSupport; public class Testaction extends Actionsupport {private static final long serialversionuid = 1L; private list<user> userlist = new arraylist<user> ()//This variable is passed as a query result set to the report to the public map<string,object> Map = new hashmap<string, Object> (); The map is used to pass values to the foreground report to assign the variable protected String imageservleturl = "//images//" in the report template; Public String Execute () {try {httpservletrequest request = Servletactioncontext.getrequest (); HttpServletResponse response = ServletactionconText.getresponse (); Userlist=new Userdao (). Getallstudent (); Map.put ("pp", "123456"); catch (Exception e) {e.printstacktrace (); return ERROR;} return SUCCESS; Public map<string, Object> Getmap () {return map;} public void Setmap (map<string, object> map) {This.map = Map Public list<user> getuserlist () {return userlist.} public void Setuserlist (list<user> userlist) {This.use Rlist = userlist; } }

6, after writing the above code, you can do a small test to prevent the problem caused by the code to fill the report failed, the following operation is critical, please note:

First, we at 1 when just add an empty JavaBean connection source, here we want to add the content of the connection source, the specific approach: the User.java according to the path of the package compiled package as in this example, the path is: Com.ansure.moudle.User. Place the packed bag in the directory folder where you store the ireport template, and then

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.