海鷗航際

          JAVA站
          posts - 11, comments - 53, trackbacks - 1, articles - 102
          <%@ 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做為數(shù)據(jù)源,這種方式比用Connection方式更為靈活方便
          主站蜘蛛池模板: 和龙市| 扎赉特旗| 弋阳县| 天峻县| 凤阳县| 奉化市| 绥滨县| 左权县| 古丈县| 呼伦贝尔市| 淄博市| 万荣县| 汝州市| 麻阳| 克东县| 壤塘县| 广灵县| 嘉兴市| 定兴县| 环江| 旬邑县| 玛沁县| 高陵县| 邢台县| 司法| 克东县| 林芝县| 江安县| 钟山县| 隆安县| 广饶县| 霍城县| 石渠县| 台前县| 保山市| 桦川县| 剑川县| 白城市| 雷波县| 汉沽区| 临沭县|