IReport + JasperReport(中)
iReport 笔记(四) 使用JDBC作为数据源<a href="http://blog.csdn.net/lldwolf/archive/2008/05/19/2458278.aspx" />
<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
4.1 定义报表
在本例中,我先使用JDBC数据库连接方式,使用数据库为MS SQL Server,打印示例数据库Northwind中的Products.ProductName列表。
iReport默认提供了MySql和HSql的驱动,如果要使用其它的数据库,需要将相应的jar包复制到iReport安装目录的lib子目录中,例如,对于SQLServer,我使用微软提供的驱动,将msbase.jar、mssqlserver.jar、msutil.jar这3个文件复制到lib子目录中。
启动iReport,点击菜单“Data”-“Connection/Data Sources”定义数据源,在此我使用“Database JDBC Connection”,中间有关的配置项不再多说,各输入框的具体含义请参考本人所写的《使用数据库》一章。本人配置画面如下所示:
http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_01.jpg
如果只定义了一个数据源,它即为默认的活动数据源,否则需要使用菜单“Data”-“Set Active Connection”设定活动数据源。
新建一个报表,在菜单“Data”-“Report Query”中设定该报表用到的SQL,我设为
select productname from products报表的布局与上例相同。
4.2 测试报表
定义完报表后,使用菜单“Build”-“Compile”进行编译,如果一切正常的话,则编译成功,编译后生成.jasper文件,可在项目中进行编程使用。选择测试方式为“Build”-“JRViewer Preview”,然后点击菜单“Execute (With activeconnection)”,即可查看测试结果,如下图所示:
http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_04.jpg
还可以使用其它的测试方式,会在当前目录下生成相应的文件,例如,我的报表文件为report1.jasper,选择PDF测试方式,则在当前目录下生成report1.pdf。常用的方式为PDF方式,其它方式结果一般不尽如人意。
4.3 编程
将编译生成的.jasper文件置于程序包中下,例如lld.test.ireport,建立如下Servlet,即可根据.jasper文件创建PDF报表
<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackage lld.test.ireport;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.io.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.sql.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.ServletException;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.http.HttpServlet;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.http.HttpServletRequest;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.http.HttpServletResponse;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport net.sf.jasperreports.engine.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport net.sourceforge.jtds.jdbc.Driver;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpublic class FirstRepServlet extends HttpServlet
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private static final long serialVersionUID = 685516851376141590L;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif @Override
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif protected void doGet(HttpServletRequest req, HttpServletResponse resp)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif throws ServletException, IOException
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.doPost(req, resp);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif @Override
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif protected void doPost(HttpServletRequest req, HttpServletResponse resp)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif throws ServletException, IOException
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif try
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif Class.forName("net.sourceforge.jtds.jdbc.Driver");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif DriverManager.registerDriver(new Driver());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif Connection connection = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/NorthWind", "sa", "sa");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String root_path = this.getServletContext().getRealPath("/");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif root_path = root_path.replace('\', '/');
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif File reportFile = new File(root_path + "/WEB-INF/classes/lld/test/ireport/report1.jasper");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif System.out.println("find file " + reportFile.getPath());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), null, connection);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif System.out.println("create pdf file stream");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif resp.setContentType("application/pdf");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif// resp.setContentType("unknown");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif// resp.addHeader("Content-Disposition", "attachment;filename="1.pdf"");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif resp.setContentLength(bytes.length);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif OutputStream os = resp.getOutputStream();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif os.write(bytes, 0, bytes.length);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif os.flush();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif os.close();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif System.out.println("close output stream.");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif } catch (Exception e)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif e.printStackTrace();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页:
[1]