Essa é uma classe que criei pra criar os reports
você pode adaptar para o seu caso.
(Desculpe o fato de não ter um JavaDoc adequado nem comentarios.
package com.jasper;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
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.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
public final class ReportMaker {
private JasperPrint jp;
public ReportMaker(Connection connection, String filePath)
throws JRException {
this(connection, filePath, new HashMap<String, String>());
}
public ReportMaker(Connection connection, String filePath,
HashMap<String, String> parameters) throws JRException {
try {
if (!filePath.endsWith(".jrxml"))
filePath += ".jrxml";
JasperReport jr = (JasperReport) JRLoader.loadObject(filePath);
jp = JasperFillManager.fillReport(jr, parameters, connection);
} catch (JRException e) {
throw e;
}
}
public ReportMaker(Connection connection, String filePath, String sql)
throws SQLException, JRException {
this(connection, filePath, new HashMap<String, String>(), sql);
}
public ReportMaker(Connection connection, String filePath,
HashMap<String, String> parameters, String sql)
throws SQLException, JRException {
try {
if (!filePath.endsWith(".jrxml"))
filePath += ".jrxml";
JasperReport jr = (JasperReport) JRLoader.loadObject(filePath);
ResultSet rs = connection.createStatement().executeQuery(sql);
JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
jp = JasperFillManager.fillReport(jr, parameters, jrRS);
} catch (JRException e) {
throw e;
} catch (SQLException e) {
throw e;
}
}
public ReportMaker(Connection connection, InputStream fileStream,
HashMap<String, String> parameters) throws JRException {
try {
JasperReport jr = (JasperReport) JasperCompileManager
.compileReport(fileStream);
jp = JasperFillManager.fillReport(jr, parameters, connection);
} catch (JRException e) {
throw e;
}
}
public ReportMaker(Connection connection, InputStream fileStream)
throws SQLException, JRException {
this(connection, fileStream, new HashMap<String, String>());
}
public ReportMaker(Connection connection, InputStream fileStream,
HashMap<String, String> parameters, String sql)
throws SQLException, JRException {
try {
JasperReport jr = (JasperReport) JasperCompileManager
.compileReport(fileStream);
ResultSet rs = connection.createStatement().executeQuery(sql);
JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
jp = JasperFillManager.fillReport(jr, parameters, jrRS);
} catch (JRException e) {
throw e;
} catch (SQLException e) {
throw e;
}
}
public ReportMaker(Connection connection, InputStream fileStream, String sql)
throws SQLException, JRException {
this(connection, fileStream, new HashMap<String, String>(), sql);
}
public void exportToHTML(String local) throws JRException {
JasperExportManager.exportReportToHtmlFile(jp, local);
}
public void exportToPdf(String local) throws JRException {
JasperExportManager.exportReportToPdfFile(jp, local);
}
public void viewReport() {
JasperViewer.viewReport(jp, false);
}
public void exportToPdfStream(OutputStream os) throws JRException {
JasperExportManager.exportReportToPdfStream(jp, os);
}
public void print(boolean prompt) throws JRException {
JasperPrintManager.printReport(jp, prompt);
}
}