JasperReport can be used as a data source with Collection

xiaoxiao2021-03-06  39

<%

@Page ContentType

=

"

Text / html; charset = GBK

"

%>

<%

@Page Import

=

"

DORI.JASPER.EENGINE. *

"

%>

<%

@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 "); // with ReportFile mode // 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 'END, " " ISFIRST = Case Caruse.Priority When 0 Then' low 'When 1 Then' the 'else' high '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.car ID 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.setusescription (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 can be used as a data source with Collection, which is more flexible and convenient than using the Connection mode.

转载请注明原文地址:https://www.9cbs.com/read-64203.html

New Post(0)