Jump to content
JasperReports Library 7.0 is now available ×

Recommended Posts

I am developing a JAVA / SWING application for the desktop with a PostgreSQL database management system. I already have several reports implemented without problems. The mentioned reports take the common fields as parameters (Logo, title, company name, publisher, etc.). All this is managed from a class (EngineReport.class). Then I have started to implement reports with a parameter (String) to apply filters to the dataset. Now I need to implement parameters for multiple filters (String, Integer, etc). I do this using a generic method of the EngineReport class. Here is the full class code:

package com.m3dsa.gci.controller;import com.m3dsa.gci.app.Inicio;import com.m3dsa.gci.model.PostgreSQL;import com.m3dsa.gci.view.Application;import java.sql.Connection;import java.sql.SQLException;import java.util.HashMap;import javax.swing.JDialog;import javax.swing.JOptionPane;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;/** * * @author Elio Gabriel Drovandini * @version 1.0 */public class EngineReports {    /**     * Constructor por defecto     */    public EngineReports() {        datos = PostgreSQL.getInstancia();        conn = datos.getConeccionOn();        parametros = new HashMap<String, Object>();    }    /**     * Constructor de clase     *     * @param recursoInforme Ruta relativa de recurso al informe de Jasper     */    public EngineReports(String recursoInforme, String titulo) {        this();        rutaRelativaRecurso = recursoInforme;        this.titulo = titulo;    }    /**     * Fija los parámetros de filtrado del informe     *     * @param <T> Parámetrización del método     * @param nombreParametro Nombre de la variable en el informe Jasper     * @param tipoParametro Tipo de datos de la variable de parámetro     */    public <T> void setParametros(String nombreParametro, T tipoParametro) {        parametros.put(nombreParametro, tipoParametro);    }        private void setCampos() {        rutaInforme = getClass().getResource(rutaRelativaRecurso).getPath().replaceFirst("/", "").replace("%20", " ");        String app_denominacion = Inicio.appNombre + " V " + Inicio.versionApp;        parametros.put("APP_DENOMINACION", app_denominacion);        parametros.put("EMPRESA_LOGO", getClass().getResourceAsStream("/images/empresa_logo.png"));        String paramDomicilio = Inicio.comDomicilio + ", " + Inicio.comLocalidad + " - " + Inicio.comProvincia + " (" + Inicio.comPais + ")";        parametros.put("EMPRESA_DOMICILIO", paramDomicilio);        parametros.put("EMPRESA_CUIT", Inicio.cuit);        parametros.put("EMPRESA_CORREO", Inicio.correo);        String telFax = Inicio.telefono + " / FAX | " + Inicio.fax;        parametros.put("EMPRESA_TELEFONOS", telFax);        parametros.put("INFORME_TITULO", titulo);        parametros.put("INFORME_EMISOR", Application.getUsuario());    }    /**     * Ejecuta el informe del cual se ha pasado su ruta relativa a los recursos     */    public void getInforme() {        /* Se personaliza el informe con los campos privados de la empresa */        setCampos();        try {            informe = (JasperReport) JRLoader.loadObjectFromFile(rutaInforme);            JasperPrint impresora = JasperFillManager.fillReport(informe, parametros, conn);            JasperViewer visor = new JasperViewer(impresora, false);            visor.setDefaultCloseOperation(JasperViewer.DISPOSE_ON_CLOSE);            visor.setTitle("GCI - Informe: " + titulo);            visor.setVisible(true);        } catch (JRException ex) {            //Logger.getLogger(InformeSinFiltros.class.getName()).log(Level.SEVERE, null, ex);            JOptionPane.showMessageDialog(null, ex.getMessage(), Inicio.appNombre, JOptionPane.ERROR_MESSAGE);        } finally {            datos.setConeccionOff();        }    }    /**     * Ejecuta el informe incrustado en un JDialog para salvar el inconveniente     * de la modalidad     */    public void getDlgInforme(JDialog base) {        /* Se personaliza el informe con los campos privados de la empresa */        setCampos();        /* Se declara el dialogo contenedor del visor de informe */        JDialog contenedor = new JDialog(base);        try {            informe = (JasperReport) JRLoader.loadObjectFromFile(rutaInforme);            JasperPrint impresora = JasperFillManager.fillReport(informe, parametros, conn);            JasperViewer visor = new JasperViewer(impresora, false);            contenedor.setContentPane(visor.getContentPane());            contenedor.setSize(visor.getSize());            contenedor.setResizable(true);            contenedor.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);            contenedor.setTitle("GCI - Informe: " + titulo);            contenedor.setVisible(true);        } catch (JRException ex) {            //Logger.getLogger(InformeSinFiltros.class.getName()).log(Level.SEVERE, null, ex);            JOptionPane.showMessageDialog(null, ex.getMessage(), Inicio.appNombre, JOptionPane.ERROR_MESSAGE);        } finally {            datos.setConeccionOff();        }    }    /**     * Gestiona la re-conexión a la BBDD     */    private void setReconexion() {        try {            if (conn.isClosed()) {                conn = datos.getConeccionOn();            }        } catch (SQLException ex) {            //Logger.getLogger(CatalogoDAO.class.getName()).log(Level.SEVERE, null, ex);            JOptionPane.showMessageDialog(null, ex.getMessage(), Inicio.appNombre, JOptionPane.ERROR_MESSAGE);        }    }    /* Declaración de campos de clase  de desarrollador */    private PostgreSQL datos;    private Connection conn;    private String rutaRelativaRecurso, rutaInforme, titulo;    private JasperReport informe;    private JasperPrint impresora;    private JasperViewer visor;    private HashMap<String, Object> parametros;    /* Fin de declaración de campos de clase de desarrollador */}[/code]

I have read all the documentation about this procedure but I get an error that I show below:

captura-019.jpg.abe54b6a71a4abcf401a71539a891338.jpg

I can't understand what is happening. I have tried changing the order of the parameters, the data type of the parameters, etc. As you can see in the exception the parameters are not taken. With the NetBeans debugger you can see that the parameter object (HashMap <String, Object>) is complete. I do not understand why it does not work if in the previous reports it does it without problems. An important observation is: in the jasperSoft Studio SQL editor when I implement the WHERE condition for the Integer parameter it doesn't appear in the list of available parameters, it only does it if I change the parameter's data type to String. Thank you in advance for your attention and I await your response

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Posted Images

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...