Jump to content
JasperReports Library 7.0 is now available ×

JRDataSource and subreports


Recommended Posts

By: jniemann42 - jniemann42

JRDataSource and subreports

2006-05-12 13:47

I'm having trouble with passing a JRDataSource to a subreport and getting the report to work or even return (JasperFillManager runs forever). I've read through all the message boards and still can't decipher how to make this work, after banging my head against a wall for a few days, I'm going to ask again.

 

I currently have a working implementation of JRDataSource and run most of my reports by loading the report and extracting the SQL query, running it, and putting the result in a JRDataSource, then running JasperFillManager like so:

 

byte[] repBytes = JRLoader.loadBytes(new URL(_serverURL + _reportURL + _reportFile));

_rep = (JasperReport) JRLoader.loadObject(new ByteArrayInputStream(repBytes));

 

Map parameters = Parameter.getParameterMap(_rep, _queryString);

 

//lots of params go here

parameters.put("sampleParam", "Jason's report");

 

long start = System.currentTimeMillis();

//run main query and extract subquery if there is one... _ds has my database connection and final SQL (after params have been entered)

String sub = _ds.setOrgJDataSource(jQuery, maxrows, querytimeout, _parent, _subQuery, _dataName, _joinName);

 

parameters.put("ReportTime", "000" + String.valueOf(_ds.getFinish() - start));

 

JRDataSource jDataSource = _ds.getOrgJDataSource();

 

_pr = JasperFillManager.fillReport(_rep, parameters, jDataSource);

 

My setOrgJDataSource runs PrepareStatement and ExecuteQuery to get a resultSet, which I use to fill a CachedResultSet, which is my class that implements JRDataSource. All of this works fine.

 

 

Now I have a need to run a report against two different databases (main one in Oracle, sub in DB2), using the first result to populate the second SQL query. I can make this work by populating a CachedRowSet with the main result set, then running the subquery and inserting it in the CachedRowSet (row by row) and passing it to a JRDataSource as in the code above (printing it in a column by using $F{SAMPLE} in my main report). However, this seems like a hack since I should be to use a real subreport to do this. Here's how I tried it:

 

run above code to get main datasource and subquery

 

if(subdbDriver != null){ _ds.closeConnection(); _ds.connect(subdbDriver, subdbURL, subdbUserId, subdbPasswd); _ds.setSubJDataSource(sub, maxrows, querytimeout, _parent, _ds.getConnection());

JRDataSource subDataSource = _ds.getOrgJDataSource(); parameters.put("subDatasource", subDataSource);

}

 

In the above code I close my original DB connection to Oracle and pass similar "set" method my completed subQuery and connection to DB2. I call ExecuteQuery and getResultSet (which I know is right, since I was able to get a correct result with my brute-force report earlier).

 

My main report JRXML has a subreport element in it that has a DatasourceExpression of $P{subDatasource} which is set above. I know the subreport location is set correctly in the subreport element. In my subreport I just display one field $F{VALUE} (a String) which should be contained in my datasource.

 

If I comment out this line:

parameters.put("subDatasource", subDataSource);

I get my normal report without the subreport, put it back in, and it seems that FillManager will run forever (I have a print statement after it finishes that never shows up).

 

Any ideas? I can provide more information if this isn't enough to see what I'm doing...

 

 

 

 

By: Lucian Chirita - lucianc

RE: JRDataSource and subreports

2006-05-16 04:44

I'm afraid you need to do more debugging/testing. You've posted quite a large amount of information, but I don't think it's possible to tell the cause of the issue from it.

 

Reports that never end filling usually have layout problems, especially when subreports are involved. But again, maybe something else is wrong in you case..

 

There are several things you can do to find the cause of the problem:

- create some scriptlet classes for both the master report and the subreport and put debug statements in all the scriptlet callback methods so that you can see the fill progress.

- put some debug statements in your datasource to see its progress.

- take your subreport and strip all the layout from it (remove all the bands). If the report runs after this, then the datasource is ok and something is wrong with the layout.

- use a JREmptyDataSource instead of your own for the subreport and see whether the fill works this way.

- and so on

 

HTH,

Lucian

 

 

 

 

By: jniemann42 - jniemann42

RE: JRDataSource and subreports

2006-05-17 11:30

Thanks for the response, I looked a little closer and realized that I wasn't trapping the Exception to where I could see it (there was an exception, but I didn't close my Swing application, so it's NOT running forever). Here is exception that I get now... I know my JRDataSource implementation is good because I can get it to run a main report with no subreport, but when I do

 

parameters.put("subDatasource", subDataSource),

 

it will crash with the error below. If I do

 

parameters.put("subDatasource", null)

_pr = JasperFillManager.fillReport(_rep, parameters, jDataSource);

 

I get my normal report without the subreport section.

 

Here's the error:

java.lang.ClassNotFoundException: JRDataSource

at com.sun.jnlp.JNLPClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at com.sun.jnlp.JNLPClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:90)

at net.sf.jasperreports.engine.base.JRBaseParameter.getValueClass(JRBaseParameter.java:129)

at net.sf.jasperreports.engine.fill.JRFillParameter.getValueClass(JRFillParameter.java:95)

at net.sf.jasperreports.engine.fill.JRFillDataset.setParameter(JRFillDataset.java:776)

at net.sf.jasperreports.engine.fill.JRFillDataset.setFillParameterValues(JRFillDataset.java:607)

at net.sf.jasperreports.engine.fill.JRFillDataset.setParameterValues(JRFillDataset.java:563)

at net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:871)

at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:686)

at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:663)

at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)

at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)

at com.lmco.jborg.ReportApplication$4.construct(ReportApplication.java:323)

at com.lmco.util.SwingWorker$2.run(SwingWorker.java:108)

at java.lang.Thread.run(Unknown Source)

 

NESTED BY :

net.sf.jasperreports.engine.JRRuntimeException: JRDataSource

at net.sf.jasperreports.engine.base.JRBaseParameter.getValueClass(JRBaseParameter.java:133)

at net.sf.jasperreports.engine.fill.JRFillParameter.getValueClass(JRFillParameter.java:95)

at net.sf.jasperreports.engine.fill.JRFillDataset.setParameter(JRFillDataset.java:776)

at net.sf.jasperreports.engine.fill.JRFillDataset.setFillParameterValues(JRFillDataset.java:607)

at net.sf.jasperreports.engine.fill.JRFillDataset.setParameterValues(JRFillDataset.java:563)

at net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:871)

at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:686)

at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:663)

at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)

at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)

at com.lmco.jborg.ReportApplication$4.construct(ReportApplication.java:323)

at com.lmco.util.SwingWorker$2.run(SwingWorker.java:108)

at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.ClassNotFoundException: JRDataSource

at com.sun.jnlp.JNLPClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at com.sun.jnlp.JNLPClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:90)

at net.sf.jasperreports.engine.base.JRBaseParameter.getValueClass(JRBaseParameter.java:129)

... 12 more

 

 

I'm going to trace the error to see what's going on, but if this looks obvious to anyone (I know it's a long shot), any help at all is appreciated.

 

Jason

 

 

 

 

By: jniemann42 - jniemann42

RE: JRDataSource and subreports

2006-05-18 04:05

And I'm an idiot...I had JRDataSource in for Value Class instead of net.sf.jasperreports.engine.JRDataSource. I guess I assumed it would have given me an error at compile time and since it didn't, that I was ok. Thanks for responding, Lucian.

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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...