4.1 定义报表
在本例中,我先使用JDBC数据库连接方式,使用数据库为MS SQL Server,打印示例数据库Northwind中的Products.ProductName列表。
iReport默认提供了MySql和HSql的驱动,如果要使用其它的数据库,需要将相应的jar包复制到iReport安装目录的lib子目录中,例如,对于SQL Server,我使用微软提供的驱动,将msbase.jar、mssqlserver.jar、msutil.jar这3个文件复制到lib子目录中。
启动iReport,点击菜单“Data”-“Connection/Data Sources”定义数据源,在此我使用“Database JDBC Connection”,中间有关的配置项不再多说,各输入框的具体含义请参考本人所写的《使用数据库》一章。本人配置画面如下所示:
![iReport 笔记(四) 使用JDBC作为数据源_第1张图片](http://img.e-com-net.com/image/info5/0ec5db98bc9c46f2a639db879fa70ba3.jpg)
如果只定义了一个数据源,它即为默认的活动数据源,否则需要使用菜单“Data”-“Set Active Connection”设定活动数据源。
新建一个报表,在菜单“Data”-“Report Query”中设定该报表用到的SQL,我设为
select productname from products
报表的布局与上例相同。
4.2 测试报表
定义完报表后,使用菜单“Build”-“Compile”进行编译,如果一切正常的话,则编译成功,编译后生成.jasper文件,可在项目中进行编程使用。选择测试方式为“Build”-“JRViewer Preview”,然后点击菜单“Execute (With active connection)”,即可查看测试结果,如下图所示:
![iReport 笔记(四) 使用JDBC作为数据源_第2张图片](http://img.e-com-net.com/image/info5/6c88d73f25ee4d558475a7622cbd2c89.jpg)
还可以使用其它的测试方式,会在当前目录下生成相应的文件,例如,我的报表文件为report1.jasper,选择PDF测试方式,则在当前目录下生成report1.pdf。常用的方式为PDF方式,其它方式结果一般不尽如人意。
4.3 编程
将编译生成的.jasper文件置于程序包中下,例如lld.test.ireport,建立如下Servlet,即可根据.jasper文件创建PDF报表
package
lld.test.ireport;
![](http://img.e-com-net.com/image/info5/48e3d422a0404483a9deefb3128506c9.gif)
import
java.io.
*
;
import
java.sql.
*
;
![](http://img.e-com-net.com/image/info5/48e3d422a0404483a9deefb3128506c9.gif)
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
net.sf.jasperreports.engine.
*
;
import
net.sourceforge.jtds.jdbc.Driver;
![](http://img.e-com-net.com/image/info5/48e3d422a0404483a9deefb3128506c9.gif)
public
class
FirstRepServlet
extends
HttpServlet
![](http://img.e-com-net.com/image/info5/7a63b3e47482464a9d6c0a7e14720343.gif)
...
{
private static final long serialVersionUID = 685516851376141590L;
![](http://img.e-com-net.com/image/info5/a346b1749c914b45a8a28b734475a530.gif)
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
![](http://img.e-com-net.com/image/info5/b188521f3aa84b57a8056a8c8b5b6aab.gif)
...{
this.doPost(req, resp);
}
![](http://img.e-com-net.com/image/info5/a346b1749c914b45a8a28b734475a530.gif)
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
![](http://img.e-com-net.com/image/info5/b188521f3aa84b57a8056a8c8b5b6aab.gif)
...{
try
![](http://img.e-com-net.com/image/info5/b188521f3aa84b57a8056a8c8b5b6aab.gif)
...{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.registerDriver(new Driver());
Connection connection = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/NorthWind", "sa", "sa");
String root_path = this.getServletContext().getRealPath("/");
root_path = root_path.replace('/', '/');
File reportFile = new File(root_path + "/WEB-INF/classes/lld/test/ireport/report1.jasper");
System.out.println("find file " + reportFile.getPath());
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), null, connection);
System.out.println("create pdf file stream");
resp.setContentType("application/pdf");
// resp.setContentType("unknown");
// resp.addHeader("Content-Disposition", "attachment;filename="1.pdf"");
resp.setContentLength(bytes.length);
OutputStream os = resp.getOutputStream();
os.write(bytes, 0, bytes.length);
os.flush();
os.close();
System.out.println("close output stream.");
} catch (Exception e)
![](http://img.e-com-net.com/image/info5/b188521f3aa84b57a8056a8c8b5b6aab.gif)
...{
e.printStackTrace();
}
}
}
注:如需本例源代码,可留言或向[email protected]索取