reportes con jasperreports

panuhaya
19 de Mayo del 2005
Hola a todos, tengo que hacer algunos reportes con jasperreports, para mostrarlos en jsp e imprimirlos también, pero no tengo ni idea como hacerlo... he tratado de ver los ejemplos pero no entiendo muy bien como compilar los .java y después los .xml o algo así, o no se si tengo que instalar algo más, el server de web es tomcat y los querys a la bd son en los servlets.

Diego Duque
19 de Mayo del 2005
Trata de bajarte estas herramientas:
- ireports: http://ireport.sourceforge.net/ te servira para diseñar tus reportes.
- jasperreports: http://jasperreports.sourceforge.net/ esto te ayudara a integrar java con tu reporte.

Lo siguiente es un ejeplo de esta integracion:

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
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;
import net.sf.jasperreports.view.JasperViewer;

/**
* <p>
* Ejemplo práctico de visualización de un reporte de JasperReports que contiene
* un subreporte.
* </p>
* <p>
* Esta clase ha sido desarrollada para ilustrar el tutorial "JasperReports,
* iReport y Subreportes" escrito para la comunidaad de javaHispano.
* </p>
*
* @author Francesc Rosés i Albiol
*/
public class Reporte {

public Reporte()
{
// Cargamos el driver JDBC
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found.");
//System.exit(1);
}

// Obtenemos las URLs del reporte maestro y del subreporte


URL urlMaestro = this.getClass().getResource(
"/reports/TUTORIAL_SUBREPORTS_MASTER.jasper");
URL urlSubreporte = this.getClass().getResource(
"/reports/TUTORIAL_SUBREPORTS_SUBREPORT.jasper");
if (urlMaestro == null) {
System.out.println("No encuentro el archivo del reporte maestro.");
System.exit(2);
}
if (urlSubreporte == null) {
System.out.println("No encuentro el archivo del subreporte.");
System.exit(2);
}

// Cargamos el reporte maestro
JasperReport masterReport = null;
try {
masterReport = (JasperReport) JRLoader.loadObject(urlMaestro);
} catch (JRException e) {
System.out
.println("Error cargando el reporte maestro: " + e.getMessage());
System.exit(3);
}

// Cargamos el subreporte
JasperReport subReport = null;
try {
subReport = (JasperReport) JRLoader.loadObject(urlSubreporte);
} catch (JRException e) {
System.out.println("Error cargando el subreporte: " + e.getMessage());
System.exit(3);
}

// Obtenemos una conexión con la base de datos
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:odbc:db_reporte");
} catch (SQLException e) {
System.out.println("Error de conexión: " + e.getMessage());
System.exit(4);
}

// Parámetros del reporte maestro
Map masterParams = new HashMap();
masterParams.put("SUBREPORT", subReport);
masterParams.put("PERSONA_ID", new Integer(1));

// Parámetros del subreporte:
// Ninguno. El parámetro PERSONA_ID, se lo pasa
// el report maestro

// Llenamos el reporte maestro (y por ende el subreporte)
// y obtenemos un objeto JasperPrint que puede ser
// visualizado, impreso o exportado
JasperPrint masterPrint = null;
try {
masterPrint = JasperFillManager.fillReport(masterReport, masterParams,
con);
} catch (JRException e) {
System.out
.println("Error llenando el reporte maestro: " + e.getMessage());
try {
con.close();
} catch (SQLException e1) {
}
System.exit(5);
}

// Visualizamos el reporte
try {
JasperViewer.viewReport(masterPrint, false);
} catch (JRException e) {
System.out.println("View report error: " + e.getMessage());
}

// Cerramos la conexión con la base de datos
try {
con.close();
} catch (SQLException e) {
// ME DA IGUAL
}
}

public static void main(String[] args)
{
new Reporte();
}
}


Espero que esto te ayude.