JasperReport

package test.report;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

public class GenerateReport {
   public static void main(String[] args) {
      try {
         sql4Report();
      } catch (JRException e) {
         e.printStackTrace();
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         System.out.print( "finish" );
      }
   }
	
   public static void parameter4Report () 
      throws SQLException, JRException {
      //report parameter
      HashMap<String, Object> parameter = 
         new HashMap<String, Object>();
      parameter.put( "master_location_id", new BigDecimal(22) );

      Connection con = getConnection();
      JasperReport jasperReport = 
         JasperCompileManager.compileReport("reports/Location.jrxml");
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( jasperReport, parameter, con );
		
      JasperExportManager.exportReportToPdfFile(
         jasperPrint, "reports/location_parameter4Report.pdf");	    
   }
	
   public static void sql4Report () throws SQLException, JRException {
      //create the ResultSet
      Connection con = getConnection();
      PreparedStatement statement = 
         con.prepareStatement( 
            "select * from loc_location "
          + "where location_type_cd = 'ROOM' "
          + "order by location_type_cd" 
         );
      ResultSet resultSet = statement.executeQuery();
		
      JRResultSetDataSource result = 
         new JRResultSetDataSource( resultSet );
      JasperReport jasperReport = 
         JasperCompileManager.compileReport("reports/Location.jrxml");
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( 
            jasperReport, new HashMap(), result 
         );
		
      JasperExportManager.exportReportToPdfFile(
         jasperPrint, "reports/location_sql4Report.pdf"
      );
   }
	
   public static void jasper4Report () 
      throws SQLException, JRException {
      //create the ResultSet
      Connection con = getConnection();
      PreparedStatement statement = 
         con.prepareStatement( 
            "select * from loc_location "
          + "where location_type_cd = 'ROOM' "
          + "order by location_type_cd" 
         );
      ResultSet resultSet = statement.executeQuery();
		
      JRResultSetDataSource result = 
         new JRResultSetDataSource( resultSet );
      //modify
      JasperReport jasperReport = 
         (JasperReport) JRLoader.loadObject(
            "reports/Location.jasper"
         );
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( 
            jasperReport, new HashMap(), result
         );
		
      JasperExportManager.exportReportToPdfFile(
         jasperPrint, "reports/location_jasper4Report.pdf"
      );
   }

   public static void xls4Report () throws SQLException, JRException {
//    create the ResultSet
      Connection con = getConnection();
      PreparedStatement statement = 
         con.prepareStatement(
            "select * from loc_location "
          + "where location_type_cd = 'ROOM' "
          + "order by location_type_cd" 
         );
      ResultSet resultSet = statement.executeQuery();
		
		
      JRResultSetDataSource result = 
         new JRResultSetDataSource( resultSet );
//    create JasperReport from .jasper
      JasperReport jasperReport = 
         (JasperReport) JRLoader.loadObject("reports/Location.jasper");
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( jasperReport, new HashMap(), result );
		
//        JasperExportManager.exportReportToPdfFile( jasperPrint, "reports/location_xls4Report.xls");
        
      JRXlsExporter xlsExporter = new JRXlsExporter();
      xlsExporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
      xlsExporter.setParameter( JRExporterParameter.OUTPUT_FILE, new File("reports/location_xls4Report.xls") );
      xlsExporter.exportReport();
   }
	
   private static Connection getConnection () throws SQLException {
      DriverManager.registerDriver( 
         new oracle.jdbc.driver.OracleDriver()
      );  
      return DriverManager.getConnection( url, user, pw );
   }
	
   private static String url = "jdbc:oracle:thin:@localhost:1521:db";
   private static String user = "report";
   private static String pw   = "report";
}


需要的包:
jasperreports-3.1.2.jar
jasperreports-3.1.2-applet.jar
jasperreports-3.1.2-javaflow.jar
commons-collections.jar
commons-digester.jar
commons-logging-1.0.4.jar
commons-logging-api.jar
commons-beanutils.jar
itext-1.3.1.jar
classes12.jar
poi-3.0.1.jar
poi-contrib-3.0.1-FINAL-20070705.jar
poi-scratchpad-3.0.1-FINAL-20070705.jar

文件结构:
+test
++report
+++GenerateReport.java

+reports
++Location.jrxml
++Location.jasper

数据表要和Location.jrmal或Location.jasper(即report的设计一致就行了)

另外可以参考一下这里:
http://blog.csdn.net/kabini/archive/2007/05/08/1600098.aspx

你可能感兴趣的:(java,oracle,sql,.net,jdbc)