Usando o Jasper Assistent atraves do Eclipse 3.2 consegui desenhar o relatorio e até exportar em varios formatos no caso .pdf, O que quero e chamar o arquivo gerado arquivo.jasper atravez do meu aplicativo. Tipo clicar em um botão e chamar o relatorio
Obrigado!
E o que não está conseguindo fazer?
é sim…
vou mostrar o código
/*
- Created on 10/02/2007
-
- To change this generated comment go to
- Window - Preferences - Java - Code Style - Code Templates
*/
package br.com.integrator.cap15;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.*;
import java.util.*;
import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.io.*;
public class SistemaRelatorio extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton btAbreRel = null;
public SistemaRelatorio() throws HeadlessException {
super();
// TODO Auto-generated constructor stub
initialize();
}
public SistemaRelatorio(GraphicsConfiguration gc) {
super(gc);
// TODO Auto-generated constructor stub
initialize();
}
public SistemaRelatorio(String title) throws HeadlessException {
super(title);
// TODO Auto-generated constructor stub
initialize();
}
public SistemaRelatorio(String title, GraphicsConfiguration gc) {
super(title, gc);
// TODO Auto-generated constructor stub
initialize();
}
private Connection getConnection( )
{
Connection con=null;
try{
String driver="org.gjt.mm.mysql.Driver";
String url= "jdbc:mysql://localhost/livraria";
String user="root";
String password="v6d3v6";
Class.forName(driver);
con = DriverManager.getConnection( url, user, password );
}
catch(ClassNotFoundException cnf){mostrarErro("Erro ao se conectar");}
catch(SQLException sqlex){mostrarErro("Erro ao se conectar");}
return con;
}
private void abreRelatorio( ) throws JRException, Exception
{
Connection con = getConnection( );
Map parameters = new HashMap();
[b]String path = "relatorios/primeiro_relatorio.jasper"[/b];
File file = new File(path);
file = file.getAbsoluteFile();
String repStr2 = file.getPath();
//mostrarErro("Path: "+file);
try{
JasperFillManager.fillReportToFile( repStr2, parameters, con );
JasperPrint jasperPrint = JasperFillManager.fillReport(repStr2,parameters,con);
JasperViewer viewer = new JasperViewer(jasperPrint, false);
viewer.setVisible(true);
}
catch(JRException jex){
mostrarErro("JasperException: "+jex.getMessage());
}
catch(Exception ex){
mostrarErro(""+ex.getStackTrace());
}
}
/**
* This method initializes btAbreRel
*
* @return javax.swing.JButton
*/
private JButton getBtAbreRel() {
if (btAbreRel == null) {
btAbreRel = new JButton();
btAbreRel.setBounds(new java.awt.Rectangle(78,58,123,40));
btAbreRel.setText("Abre Relatório");
btAbreRel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e){
try{
abreRelatorio( );
}
catch(JRException jre){
mostrarErro("JasperException: "+jre.getMessage());
}
catch(Exception ex){
mostrarErro(""+ex.getStackTrace());
}
}
});
}
return btAbreRel;
}
/**
* @param args
*/
public static void main(String[] args) {
new SistemaRelatorio().setVisible(true);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("Relatório de Livros");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getBtAbreRel(), null);
}
return jContentPane;
}
private void mostrarErro(String msg) {
JOptionPane.showMessageDialog(
null,
msg,
"Erro encontrado",
JOptionPane.ERROR_MESSAGE);
}
}