--
Você está recebendo esta mensagem porque se inscreveu no grupo "CEJUG" dos Grupos do Google.
Para cancelar a inscrição neste grupo e parar de receber seus e-mails, envie um e-mail para cejug+un...@googlegroups.com.
Para postar neste grupo, envie um e-mail para ce...@googlegroups.com.
Visite este grupo em http://groups.google.com/group/cejug.
Para obter mais opções, acesse https://groups.google.com/groups/opt_out.
public class ControleRelatorio { // Para pegar o FacesContext private FacesContext facesContext = FacesContext.getCurrentInstance(); /*Retorna o caminho "Path" da Pasta Relatorio! Por padrão coloco uma pasta 'relatorios' dentro de WebContent. */ private String relatorioPath = this.facesContext.getExternalContext() .getRealPath("relatorios") + File.separator; /* Método para Gerar Relatório! *tipoRelatorio: Coloco aqui o mesmo nome que deu no arquivo .jasper ex: teste.jasper *parametro: passo o Hasmap com os parâmetros que usarei no relatório! */ @SuppressWarnings({ "rawtypes", "unchecked" }) public String gerarRelatorio(String tipoRelatorio) { Connection conn = null; JasperPrint jPrint = null; try { //Conexão JDBC conn = ConnectionFactory.getConnectionJDBC(); //Ler o arquivo .Jasper JasperReport jReport = (JasperReport) JRLoader .loadObject(this.relatorioPath + tipoRelatorio + ".jasper"); //não estou colocando parâmetro por isso coloquei 'null' jPrint = JasperFillManager.fillReport(jReport, null, conn); //convert para pdf. JasperExportManager.exportReportToPdfFile(jPrint, this.relatorioPath + "pdf" + File.separator + tipoRelatorio + ".pdf"); File file = new File(this.relatorioPath + "pdf" + File.separator + tipoRelatorio + ".pdf"); file.deleteOnExit(); JasperViewer.viewReport(jPrint, true); Desktop.getDesktop().open(new File(this.relatorioPath + "pdf" + File.separator + tipoRelatorio + ".pdf")); } catch (Exception e) { e.printStackTrace(); try { conn.close(); } catch (SQLException exception) { exception.printStackTrace(); } } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return null; } }Ex de Uso:ControleRelatorio cr = new ControleRelatorio();cr.gerarRelatorio("relatorioSaidasPeriodo");
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// pronto. posso usar a conexão
}
});