Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:22:00+00:00 2026-05-31T19:22:00+00:00

in my Java project I have a lot of JasperReports reports with complex SQL

  • 0

in my Java project I have a lot of JasperReports reports with complex SQL queries, containing a lot of parameters. The reports are used to produce pdf documents containing the data returned by the query, grouped and formatted in various ways.

Now I also have the need to export directly the query result (e.g. a ResultSet, or a Map or a csv file, or similar…).
Is it possible to ask JasperReports to execute only the query and return results instead of rendering the pdf page?

(NOTE: it’s not the same as choosing a csv output format to the report rendering, because this method tries to convert the report design to a csv file… Instead, I’d like only to “reuse” the query inside a report, also taking advantage of JR parameters management, etc…)

This is my Java code to produce a pdf document from a report:

JasperReport report = (JasperReport) JRLoader.loadObject(inStream);
JasperPrint jasperprint = JasperFillManager.fillReport(report, params, conn);
JRAbstractExporter exporter = new JRPdfExporter();
exporter.exportReport();
ByteArrayOutputStream os = (ByteArrayOutputStream) exporter.getParameter(JRExporterParameter.OUTPUT_STREAM);
byte[] formattedReportBytes = os.toByteArray();
return formattedReportBytes;

I saw there’s a class called JRJdbcQueryExecuter inside JasperReports…
Is it possible to call it directly instead of calling fillReport, in order to get the ResultSet of the executed SQL query?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:22:01+00:00Added an answer on May 31, 2026 at 7:22 pm

    I would like to start with that this feels wrong and hacky, but it is possible, minus actually having JasperReports executing the query.

    JasperReport report = (JasperReport) JRLoader.loadObject(inStream);
    
    //this is the actual query in the report
    JRQuery query = report.getMainDataSet().getQuery;
    
    //once here you get the entire sql string, this will have any parameters replaced with 
    //the '?' character
    String queryString = query.getText();
    
    //now start building your prepared statement, I am assuming you already have your
    //connection in the conn variable
    PrepararedStatment statement = con.prepareStatement(queryString);
    
    //almost there, need to set the parameters
    //the sql query is broke up into chunks inside the JRQuery. The chunks have types 
    //that are  either text, parameter, or parameter clause. We care about parameter, 
    //not sure what parameter clause would be to be honest
    int index = 0; //this is the index to set the parameter at in the statement
    for (JRQueryChunk chunk : query.getChunks()){
         if (chunk.getType() == JRQueryChunk .TYPE_PARAMETER){
             statement.setObject(index, params.get(chunk.getText()));
             index = index + 1;
         }
    }
    //then execute the query
    ResultSet results = statement.executeQuery();
    

    Note: There is no error checking here, and you should add that. Also not sure if doing this is a great idea. It could be better to move the queries out of the reports and into your java code altogether. Then just pass in the ResultSet as a datasource and you are good to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java project with a lot java files. So, I want to
I have a Java project with a lot of classes. Now I want to
I work on an open-source Java project, and we have a lot of resource
I have a Java project that currently has a lot of JARs in its
I have a problem with printing PDF file from a Java project that is
I have Java project built with Maven2. There is used JUnit framework for testing
Here is the deal, in my Java project I have to make a composite
I have a Java project that needs a addon interface. I was thinking about
I have a Java project in Eclipse with ~10 packages and ~10 class files
I have made a java project and want to deliver it to a client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.