Struts2 + JasperReports4 + DB2 Demo

详细内容见Struts2___JasperReports4___DB2_Demo.rar

内容截图:


Struts2 + JasperReports4 + DB2 Demo_第1张图片

 

 


struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<!-- 
		(1)extends 加入 jasperreports-default
		(2)result 加入  type="jasper"
		(3)参数使用说明
		   location 指通过Ireport生成.jasper文件的路径
	       datasource 如果是javabean source,使用此参数作为数据源。
	       connection 如果是jdbc source,则使用此参数作为数据源。
	       format 生成格式,XLS,HTML.CSV,默认为PDF
	 -->
	<package name="default" namespace="/"
		extends="struts-default,jasperreports-default">
		<action name="GeneratePDF" class="com.test.TestAction">
			<result name="success" type="jasper">
				<param name="location">/jasper/report1.jasper</param>
				<param name="connection">conn</param>
				<!-- <param name="dataSource">your javabean datasource</param> -->
				<param name="format">PDF</param>
			</result>
		</action>
	</package>

</struts>

 

 

TestAction.java

 

package com.test;

import java.sql.Connection;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport
{
    private static final long serialVersionUID = 8006993549943492955L;
    
    private Connection conn;
    
    @Override
    public String execute()
        throws Exception
    {
        /*
         * 在页面跳转之前将Connection赋值
         */
        setConn(DBConnection.getInstance());
        return SUCCESS;
    }
    
    public Connection getConn()
    {
        return conn;
    }
    
    public void setConn(Connection conn)
    {
        this.conn = conn;
    }
    
}

 

 

DBConnection.java

 

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 用JDBC简单获取一个Connection连接提供给JasperReport使用
 * @author tianjun
 */
public class DBConnection
{
    private static Connection instance = null;
    
    private DBConnection()
    {
    }
    
    public static Connection getInstance()
        throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
    {
        if (instance == null)
        {
            instance = getConn();
        }
        return instance;
    }
    
    public static Connection getConn()
        throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
    {
        
        String driver = "com.ibm.db2.jcc.DB2Driver";
        String url = "jdbc:db2://192.168.1.66:50000/SAMPLE";
        String userName = "db2admin";
        String passWord = "db2admin";
        
        Connection conn = null;
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url, userName, passWord);
        
        return conn;
    }
}
 

 

你可能感兴趣的:(java,sql,struts,jdbc,db2)