海鸥航迹

学习之笔记,好文之收集。

导航

jasperreport可以用Collection做为数据源,这种方式比用Connection方式更为灵活方便

<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="dori.jasper.engine.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.zx.report.util.CarUse"%>
<%@ page import="dori.jasper.engine.data.*" %>
<%
Connection conn 
= null;
Statement st 
= null;
ResultSet rs 
= null;
byte[] bytes = null;
List carUseList
=new ArrayList();

try{

Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn 
=DriverManager.getConnection("jdbc:microsoft:sqlserver://10.10.10.207:1433;DatabaseName=report;user=sa;password=111111");

//以输入流方式
   InputStream reportFile = getClass().getClassLoader().getResourceAsStream("carUserBd.jasper");

//以reportFile方式
//File reportFile =new File(request.getRealPath("/report/carUserBd.jasper"));

Map parameters 
= new HashMap();


String sqlCarUser
="SELECT car.carNo, dept.Name , people.peopleName,carUse.useDescription ," +
" carUse.beginTime , carUse.planEndTime , carUse.comment , carUse.driver," +
" carUse.peopleNumber ,OneWay= case carUse.isOneWay  when 0 then '双程'  else '单程'  end, " +
"isFirst= case carUse.priority when 0  then '低' when  1  then  '中'  else  '高'  end, carUse.start , " +
"carUse.destination , people.peopleName as ratifier FROM DEPT_CARUSELOG carUse , DEPT_CARINFO car , " +
"ADMIN_ORGANIZATION dept , PUBLIC_PEOPLEINFO people WHERE carUse.id=290 and " +
"caruse.carId=car.carId and carUse.departId=dept.id and carUse.peopleId=people.peopleId";

st 
= conn.createStatement();
rs 
= st.executeQuery(sqlCarUser);

while(rs.next()){
CarUse carUse
=new CarUse();
carUse.setCarNo(rs.getString(
1));
carUse.setName(rs.getString(
2));
carUse.setPeopleName(rs.getString(
3));
carUse.setUseDescription(rs.getString(
4));
carUse.setBeginTime(rs.getDate(
5));
carUse.setPlanEndTime(rs.getDate(
6));
carUse.setComment(rs.getString(
7));
carUse.setDriver(rs.getString(
8));
carUse.setPeopleNumber(rs.getString(
9));
carUse.setOneWay(rs.getString(
10));
carUse.setIsFirst(rs.getString(
11));
carUse.setStart(rs.getString(
12));
carUse.setDestination(rs.getString(
13));
carUse.setRatifier(rs.getString(
14));

carUseList.add(carUse);

}


JRBeanCollectionDataSource ds 
= new JRBeanCollectionDataSource(carUseList);

bytes 
=
JasperRunManager.runReportToPdf(
reportFile,
parameters,
ds);
String fileName 
= "test.pdf";
response.setContentType(
"application/pdf");
response.addHeader(
"Content-Disposition",
"attachment;filename=\"" + fileName);

response.setContentLength(bytes.length);
ServletOutputStream ouputStream 
= response.getOutputStream();

System.
out.println("5");
ouputStream.write(bytes, 
0, bytes.length);
ouputStream.flush();
ouputStream.close();

}
 catch (SQLException sqle) {

System.
out.println("SQLException:" + sqle.getMessage());

}
 catch (JRException e) {
e.printStackTrace();
}
catch(ClassNotFoundException cnfe){


}


%>
jasperreport可以用Collection做为数据源,这种方式比用Connection方式更为灵活方便

posted on 2005-01-07 18:24  海天一鸥  阅读(4507)  评论(5编辑  收藏  举报