开发者

How to integrate JasperViewer with Swing based application

开发者 https://www.devze.com 2022-12-24 19:01 出处:网络
can I integr开发者_开发技巧ate the JasperReports Viewer to my Swing application as like if I click on view report button from my application then viewer should be opened. If so could you advise me wit

can I integr开发者_开发技巧ate the JasperReports Viewer to my Swing application as like if I click on view report button from my application then viewer should be opened. If so could you advise me with code snippet for this integration and in this viewer the save as type should be restricted for PDF only rather than every other download option filter available.

Please advice me in this regard.


Yes you can, Jasper Reports come with a usable (albeit simple) report viewer:

...
           JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
           JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport...);
           JasperViewer.viewReport(jasperPrint);
...

The last line is what displays the viewer. I'm not sure if you can restrict the available output formats to PDF, though.


Let's suppose that report contains the name of your report file (*.jrxml). This program will fill the report with data from the mysql database. I hope this solves your problem.

public void generate(String report) { // report will contain the name of your file
    try {
        InputStream design = getClass().getResourceAsStream(report);
        JasperDesign jasperdesing = JRXmlLoader.load(design);
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperdesing);
        Connection con = ConnectDB.connect();// connect function in ConnectDB calss is used to connect to the database
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
        JasperViewer.viewReport(jasperPrint, false);
    } catch (Exception e) {
        System.out.println("Exception " + e);
    }

}
public class ConnectDB {
public static Connection con = null;
public static Connection connect(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql:///dbname","username","password");
    }catch(Exception e){
        System.out.println();
    }
    return con;
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号