How to integrate JasperViewer with Swing based application

前端 未结 2 1007

can I integrate 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 advis

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 22:09

    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;
    }
    

    }

提交回复
热议问题