2010年2月22日 星期一

ERP JR DataSource

1. package test.report;
2.
3. import java.math.BigDecimal;
4. import java.sql.Connection;
5. import java.sql.DriverManager;
6. import java.sql.PreparedStatement;
7. import java.sql.ResultSet;
8. import java.sql.SQLException;
9. import java.util.HashMap;
10.
11. import net.sf.jasperreports.engine.JRException;
12. import net.sf.jasperreports.engine.JRResultSetDataSource;
13. import net.sf.jasperreports.engine.JasperCompileManager;
14. import net.sf.jasperreports.engine.JasperExportManager;
15. import net.sf.jasperreports.engine.JasperFillManager;
16. import net.sf.jasperreports.engine.JasperPrint;
17. import net.sf.jasperreports.engine.JasperReport;
18. import net.sf.jasperreports.engine.util.JRLoader;
19.
20. public class GenerateReport {
21. public static void main(String[] args) {
22. try {
23. sql4Report();
24. } catch (JRException e) {
25. e.printStackTrace();
26. } catch (SQLException e) {
27. e.printStackTrace();
28. } finally {
29. System.out.print( "finish" );
30. }
31. }
32.
33. public static void parameter4Report ()
34. throws SQLException, JRException {
35. //report parameter
36. HashMap parameter =
37. new HashMap();
38. parameter.put( "master_location_id", new BigDecimal(22) );
39.
40. Connection con = getConnection();
41. JasperReport jasperReport =
42. JasperCompileManager.compileReport("reports/Location.jrxml");
43. JasperPrint jasperPrint =
44. JasperFillManager.fillReport( jasperReport, parameter, con );
45.
46. JasperExportManager.exportReportToPdfFile(
47. jasperPrint, "reports/location_parameter4Report.pdf");
48. }
49.
50. public static void sql4Report () throws SQLException, JRException {
51. //create the ResultSet
52. Connection con = getConnection();
53. PreparedStatement statement =
54. con.prepareStatement(
55. "select * from loc_location "
56. + "where location_type_cd = 'ROOM' "
57. + "order by location_type_cd"
58. );
59. ResultSet resultSet = statement.executeQuery();
60.
61. JRResultSetDataSource result =
62. new JRResultSetDataSource( resultSet );
63. JasperReport jasperReport =
64. JasperCompileManager.compileReport("reports/Location.jrxml");
65. JasperPrint jasperPrint =
66. JasperFillManager.fillReport(
67. jasperReport, new HashMap(), result
68. );
69.
70. JasperExportManager.exportReportToPdfFile(
71. jasperPrint, "reports/location_sql4Report.pdf"
72. );
73. }
74.
75. public static void jasper4Report ()
76. throws SQLException, JRException {
77. //create the ResultSet
78. Connection con = getConnection();
79. PreparedStatement statement =
80. con.prepareStatement(
81. "select * from loc_location "
82. + "where location_type_cd = 'ROOM' "
83. + "order by location_type_cd"
84. );
85. ResultSet resultSet = statement.executeQuery();
86.
87. JRResultSetDataSource result =
88. new JRResultSetDataSource( resultSet );
89. //modify
90. JasperReport jasperReport =
91. (JasperReport) JRLoader.loadObject(
92. "reports/Location.jasper"
93. );
94. JasperPrint jasperPrint =
95. JasperFillManager.fillReport(
96. jasperReport, new HashMap(), result
97. );
98.
99. JasperExportManager.exportReportToPdfFile(
100. jasperPrint, "reports/location_jasper4Report.pdf"
101. );
102. }
103.
104. public static void xls4Report () throws SQLException, JRException {
105. // create the ResultSet
106. Connection con = getConnection();
107. PreparedStatement statement =
108. con.prepareStatement(
109. "select * from loc_location "
110. + "where location_type_cd = 'ROOM' "
111. + "order by location_type_cd"
112. );
113. ResultSet resultSet = statement.executeQuery();
114.
115.
116. JRResultSetDataSource result =
117. new JRResultSetDataSource( resultSet );
118. // create JasperReport from .jasper
119. JasperReport jasperReport =
120. (JasperReport) JRLoader.loadObject("reports/Location.jasper");
121. JasperPrint jasperPrint =
122. JasperFillManager.fillReport( jasperReport, new HashMap(), result );
123.
124. // JasperExportManager.exportReportToPdfFile( jasperPrint, "reports/location_xls4Report.xls");
125.
126. JRXlsExporter xlsExporter = new JRXlsExporter();
127. xlsExporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
128. xlsExporter.setParameter( JRExporterParameter.OUTPUT_FILE, new File("reports/location_xls4Report.xls") );
129. xlsExporter.exportReport();
130. }
131.
132. private static Connection getConnection () throws SQLException {
133. DriverManager.registerDriver(
134. new oracle.jdbc.driver.OracleDriver()
135. );
136. return DriverManager.getConnection( url, user, pw );
137. }
138.
139. private static String url = "jdbc:oracle:thin:@localhost:1521:db";
140. private static String user = "report";
141. private static String pw = "report";
142. }

沒有留言: