在 jasper 报告中插入查询

发布于 2024-12-08 21:57:39 字数 48 浏览 0 评论 0原文

是否可以在报告生成期间在 IReports/jasper 报告中执行“插入查询”?

Is it possible to execute "insert query" in IReports/jasper reports during report generation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟到的我 2024-12-15 21:57:39

是的,您需要的想法是使用以下语法的参数:$P!{PARAM_NAME}

因此,您的整个 SQL 查询(或其他类型的查询)可以简单地是 $P!{SQL}。然后您准确地传入您需要的动态 SQL。

更新:
读完Sharad的评论后,我意识到我上面的回答不好。我写的是事实……但它没有解决核心问题。

不,您的报表无法真正执行插入语句。严格来说,我确信这并非不可能。您可以在 .jar 文件中添加脚本或自定义函数来建立连接并执行插入。但实际上......一份报告将执行一个或多个查询。 JR 框架并不旨在执行插入或更新。

Yes, the idea you need is parameters using this syntax: $P!{PARAM_NAME}.

So your entire SQL query (or other type of query) could be simply $P!{SQL}. Then you pass in exactly the dynamic SQL that you need.

UPDATE:
After reading Sharad's comment, I realized that my answer above is not good. What I wrote is true... but it fails to address the core question.

No, your report cannot really execute an insert statement. Strictly speaking, I'm sure it's not impossible. You could add a scriptlet or custom function in a .jar file that makes a connection and does an insert. But realistically speaking... a report will execute one or more queries. The JR framework is not intended to execute inserts or updates.

一袭白衣梦中忆 2024-12-15 21:57:39

是的,你可以。当您想要显示报告时,可以执行查询。这是一个适合我的示例。

    try {
        Map parameters = new HashMap();
        connectionString ="jdbc:mysql://localhost/myDb", "myUsername", "myPassword"
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(connectionString);
        PreparedStatement stmt = conn.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();
        JRResultSetDataSource rsdt = new JRResultSetDataSource(rs);

        JasperPrint jp;
        jp = JasperFillManager.fillReport("sourceFileName.jasper", parameters, rsdt);
        JasperViewer jv = new JasperViewer(jp, false);
        jv.setVisible(true);

    } catch (ClassNotFoundException | SQLException | JRException ex) {
        ex.printStackTrace();
    }

Yes you can. You can execute the query when you want to display the report. Here is a sample that works for me.

    try {
        Map parameters = new HashMap();
        connectionString ="jdbc:mysql://localhost/myDb", "myUsername", "myPassword"
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(connectionString);
        PreparedStatement stmt = conn.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();
        JRResultSetDataSource rsdt = new JRResultSetDataSource(rs);

        JasperPrint jp;
        jp = JasperFillManager.fillReport("sourceFileName.jasper", parameters, rsdt);
        JasperViewer jv = new JasperViewer(jp, false);
        jv.setVisible(true);

    } catch (ClassNotFoundException | SQLException | JRException ex) {
        ex.printStackTrace();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文