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

How to Use Java Bean Datasource with Jasper Server


ycohen

Recommended Posts

I've got a Collection of Java objects with getters that I want to use to populate a report in Jasper Server. I've searched and found http://enholm.net/index.php/blog/jasper-reports-custom-data-source-example/, but it uses an applicationContext.xml which is different from the one that I've got in JasperServer, and I don't know how to adjust it.

How would I use a Collection in JasperServer?

Link to comment
Share on other sites

  • 3 weeks later...
  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

Hi,

What you need to do is follow the steps described -

1. First you need to create bean like:

 

public class PersonBean
{
private String name = "";
private int age = 0;
public PersonBean(String name, int age)
{
this.name = name;
this.age = age;
}
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
}
2. Then you need to make a class that returns a collection fo example:
 
public class Factory
{
public static java.util.Collection generateCollection()
{
java.util.Vector collection = new java.util.Vector();
collection.add(new PersonBean("Ted", 20) );
collection.add(new PersonBean("Jack", 34) );
collection.add(new PersonBean("Bob", 56) );
collection.add(new PersonBean("Alice",12) );
collection.add(new PersonBean("Robin",22) );
collection.add(new PersonBean("Peter",28) );
return collection;
}
}

3. Then you need to compile it and add it as an jar  in the folder structure(C:Program Filesjasperreports-server-x.xapache-tomcatlib.).

4. Then while defining the datasource you need to set the following based on the above snippet and set the values as--

 

Factory class: Factory
Method to call: generateCollection
Return type: Collection of JavaBean
 
Then you can test the connection and you are good to go
 
I hope that above explanation helps your cause.
 
Regards,
Ankur

 

Link to comment
Share on other sites

  • 1 month later...
  • 5 months later...

Hi,

I got the same 'Connection failed' error. I created Bean Data source in JasperReports Server, and added following information:

Bean Name: Factory

Bean Method: generateCollection

But I couldn't see 'ReturnType' field. I am using JasperServer 5.5.0.

 

Would really appreciate nay help.

Link to comment
Share on other sites

Hi folks,

Sorry for the trouble actually there is a step missed, 

after the step 3,

You need to add the Jar file to the classpath also.

you could add in the classpath by, Tools(in the Menu bar) --> select the Options --> Select the tab CLASSPATH  --> Click on Add Jar

 

One thing more I want to clearify is that you need to put both these bean as well as the static factory function in one single java class and access it in setting up the datasource. So suppose you have the package name as com.sample.jasper and your class containing both the bean as well as the factory function is JasperBeanData so the expression for the 

Bean: com.sample.jasper.JasperBeanData

Factory Class : generateCollection.

 

Hope that this helps establishing the connection.

Please do reply if more clearifications are required.

 

Thanks,

Ankur Gupta

 

Link to comment
Share on other sites

  • 1 month later...

Hi,

I have added the jar in resource to the report in jasperserver. Also have created the jrxml in ireport having only two fields i.e name and age. While adding data sources i am getting connection failed. I have deployed jasperserver in jboss. Also addded that jar in lib of jboss. What may be the reason of getting connection failed. Any help is appreciated.

Thanks in advance

Link to comment
Share on other sites

  • 2 years later...

Im also having somewhat the same issues (in 6.2.1), i created a detailed step by step of what i did.

 

http://stackoverflow.com/questions/36443248/how-can-jaspersoft-bean-datasource-be-deployed-in-jasper-server

 

does it need to put the jar file in the /lib, the add the jar as a report resource? isnt this redundant? and also, i tried this and still the bean is not recognized.

 

Link to comment
Share on other sites

  • 4 months later...

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