Jump to content
We've recently updated our Privacy Statement, available here ×

rbojja

Recommended Posts

Forgot the attachment

Hi
i am generating subreport.But the output i see is blank pdf.

please see the attachment for my jrml/jasper files.

java code is as follows.

JasperDesign jasperDesign = JasperManager.loadXmlDesign("src/Salett.jrxml");
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);
JasperDesign jasperDesign1 = JasperManager.loadXmlDesign("src/SaleReturnExchange_subreport1.jrxml");
JasperCompileManager.compileReportToFile(jasperDesign1,"src/SaleReturnExchange_subreport1.jasper");
JasperReport jasperReport1 = JasperManager.compileReport(jasperDesign1);


Map parameters = new HashMap();
parameters.put("Report1","src/SaleReturnExchange_subreport1.jasper");


can any one correct me.

Post edited by: rbojja, at: 2008/07/11 11:55 [file name=New_Folder-c04373bdb683d6e87df8048a69c23ad6.zip size=12232]

Link to comment
Share on other sites

  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I've modified your jrxml files, and wrote a servlet, which should run it, if you set the database connection properly, and place the jasper files to a place where they can be get by the ServletContext.

What did I modify:
I added a parameter named SubreportParam, of type net.sf.jasperreports.engine.JasperReport to the master report.

I set this parameter as Subreport Expression of the subreport element (and made sure, that it's type, the Subreport Expression Class, is set to net.sf.jasperreports.engine.JasperReport).

In the Java code I create a net.sf.jasperreports.engine.JasperReport object instance from the subreport, and put it into the parameters Map.

And that's all, it should fly (hope, hope).

Good Luck,
Arpad



[file name=rbojja.zip size=26274]
Post edited by: szaboaz, at: 2008/07/11 14:06

Link to comment
Share on other sites

Here you are.

This time I created a masterreport.jrxml and a subreport.jrxml, and used a MySQL connection to actually make them run, and they did!

The reports demonstrate essentially the same "feature" as your files modified previously by me: masterreport has a "SubreportParameter" parameter of type JasperReport, this is used in the subreport element of the masterreport as Subreport Expression.

It's your turn now to apply it to your reports (and your database connection, of course) :)

Good Luck,
Arpad [file name=notaservletthistime.zip size=3968]

Link to comment
Share on other sites

Hi,

 

In line

subreport = (JasperReport) JRLoader.loadObjectFromLocation("subreport.jasper");

 

and

 

JasperRunManager.runReportToPdfFile("masterreport.jasper", parameters, connection);

 

why are we using .jasper? what if we use .jrxml.

 

can you explain a bit on this.

Link to comment
Share on other sites

Using .jasper files and not .jrxml makes the code faster, since it doesn't need to do the jrxml->jasper transformation each and every time your report runs. If your report runs thousands and thousands of times, it can cause significant decrease in performance.

 

However, if your code needs to be executed rather rarely, BUT your reports change often, and you have to deploy them again and again, it might be reasonable to use .jrxml files in the code, since you don't need to compile them into .jasper files before you deploy them.

 

Code:


package subreportjasperrunner;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
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.JasperReport;

/**
*
* @author szaboaz
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver"«»);

Connection connection;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/bank", "root", "password"«»);

JasperReport subreport;
JasperReport masterreport;

try {
subreport = JasperCompileManager.compileReport("subreport.jrxml"«»);
masterreport = JasperCompileManager.compileReport("masterreport.jrxml"«»);

Map parameters = new HashMap();
parameters.put("SubreportParameter", subreport);

//It can be done more than one way:

//I choose to follow runReportToPdfFile method's code
//in JasperRunManager.java (in fact, I copied it from there).
//But I use a fix string for output filename)
//If a previous file exists with the same filename, it will be overwritten.
JasperPrint jasperPrint = JasperFillManager.fillReport(masterreport, parameters, connection);
JasperExportManager.exportReportToPdfFile(jasperPrint, "masterreport.pdf"«»);

//Another approach:

//this time, I didn't use JasperRunManager.runReportToPdfFile,
//since it cannot use a JasperReport as parameter.
//runReportToPdf, however, could take a JasperReport object
//as parameter, and it returns a byte array:

//byte[] pdf = JasperRunManager.runReportToPdf(masterreport, parameters, connection);

//you can do whatever you want to do with your byte array
//e.g. write it into a file (tricky part to decide, what to do with
//the filenames, should it overwrite or not, if it already exists
//or apply some logic to save the file with some differences in the
//name all the time...

} catch (JRException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}


} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}


} catch (ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

}

}

Link to comment
Share on other sites

I was curious, how much the difference can be between the two methods (starting with .jrxml, or starting with .jasper).

 

In my case, these were the times, when I made them run five times:

 

.jasper:

1953

1859

1843

1937

1969

 

.jrxml:

3250

3328

3296

3406

3344

 

Convincing, isn't it?

 

You can do the same by putting these two lines to your code:

 

Code:


long start = System.currentTimeMillis();

System.err.println("Filling time : " + (System.currentTimeMillis() - start));

 

Best regards,

Arpad

 

Post edited by: szaboaz, at: 2008/07/12 08:11

Post edited by: szaboaz, at: 2008/07/12 08:13

Link to comment
Share on other sites

Hi,

Thanks for you inputs, already compiled .jasper is the one which gives fast output.

 

i have one more doubt..passing of parameters to master and subreports.

 

How i need to pass params transactionId and store to both the master and subreports.

 

Both uses same parameters.

 

do i explicitly pass these parameters.

Link to comment
Share on other sites

For sure.

 

The idea behind subreports is that there is a changing data element in every record of the masterreport's data source, and a subreport usually depends on this data (takes it as a parameter, and gets its value from the query's result). This is the most typical usage of subreports.

 

Then, a masterreport can also pass a non-changing value to the subreport, if it is required.

 

So the masterreport has to get this value from somewhere. And where should it get from? Of course from a parameter.

 

So yes, if you want to give a parameter value to a subreport, you have to "inject" it into the masterreport, which forwards it to the subreport to the subreport's own parameter - whether the masterreport actually use it or not.

 

Best regards,

Arpad

 

ps. All the things I said before is said "AFAIK", I don't consider myself an authentic source ;-). If I'm wrong, please correct me. Thanks.

Link to comment
Share on other sites

Hi,
One thing i noticed a bit late.The subreport values are not being shown up on the report.

Only master report values are shown

whats the issue? where am i going wrong?

Here you find the xml in attachment [file name=sub.zip size=3482]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...