JasperReport初级教程(2.0.3)调用Java代码_图文详解_绝对原创
如需转载,请注明出处,http://www.川江号子.cn 。
另,本人有Word电子文档格式,如需要,请联系本人:[email protected]
<%@
page
contentType
=
"text/html;charset=GB2312"
%>
<%@
page
import
=
"net.sf.jasperreports.engine.*"
%>
<%@
page
import
=
"java.util.*"
%>
<%@
page
import
=
"java.io.*"
%>
<%@
page
import
=
"java.sql.*"
%>
<%
//
报表编译之后生成的
.jasper
文件的存放位置
File reportFile =
new
File(
this
.getServletContext().
(1)
getRealPath(
"/report/sample.jasper"
));
String url=
"jdbc:mysql://localhost:3306/db"
;
Class.forName(
"com.mysql.jdbc.Driver"
);
Map parameters =
new
HashMap();
//"SQLSTR"
是报表中定义的参数名称
,
其类型为
String
型
//
设置
SQLSTR
参数的内容
,
根据需要赋值
sql
语句
parameters.put(
"SQLSTR"
,
"select * from employee"
);
(2)
Connection conn = DriverManager.getConnection(url,
(3)
"username"
,
"password"
);
JasperRunManager.runReportToHtmlFile(reportFile.getPath(),
(4)
parameters,conn);
response.sendRedirect(
"report/sample.html"
);
(5)
%>
|
<%@
page
contentType
=
"application/pdf;charset=UTF-8"
%>
<%@
page
import
=
"net.sf.jasperreports.engine.*"
%>
<%@
page
import
=
"java.util.*"
%>
<%@
page
import
=
"java.io.*"
%>
<%@
page
import
=
"java.sql.*"
%>
<%
//
报表编译之后生成的
.jasper
文件的存放位置
File reportFile =
new
File(
this
.getServletContext().
getRealPath(
"/report/sample.jasper"
));
String url=
"jdbc:mysql://localhost:3306/db"
;
Class.forName(
"com.mysql.jdbc.Driver"
);
Map parameters =
new
HashMap();
//"SQLSTR"
是报表中定义的一个参数名称
,
其类型为
String
型
parameters.put(
"SQLSTR"
,
"select * from employee where employee_id like 'Z%'"
);
Connection conn = DriverManager.getConnection(url,
"username"
,
"password"
);
byte
[] bytes=JasperRunManager.
runReportToPdf(reportFile.getPath(),parameters,conn);
response.setContentType(
"application/pdf"
);
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes,0,bytes.length);
outStream.flush();
outStream.close();
out.clear();
out = pageContext.pushBody();
%>
|