Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Building reports with Java?

  • 21-05-2014 8:55am
    #1
    Registered Users, Registered Users 2 Posts: 5,267 ✭✭✭


    Hi all,

    I was wondering if anyone could help me. I'm currently working on a Java project at home that is a financial application of sorts. I'm a beginner to programming, having just completed year 1 of a 4 year degree. I've covered the basics of OOP but nothing advanced and nothing on databases just yet.

    Anyway I've build the rest of the app which allows people to enter payments and receipts for a charity and saves this info. What I need to do is output this to a PDF report.

    But I'm not entirely sure how. I read up on Jasper Reports/iReport and I've completed a report template using iReport. But I'm not sure how to link it to the application. Can anybody assist?

    The report template includes tables which need to be populated with figures. The figures are ready to go I just need to call a method in a specific class and pass in a String.

    How do I link the report to the application? I had read I need to create a Java Bean - this seems to be just a simple class with getters/setters (which I already have). What are the next steps? Or is there a different way?

    Any help appreciated!


Comments

  • Registered Users, Registered Users 2 Posts: 11,749 ✭✭✭✭wes


    You need to put the relevant jar on your build path. If your using Eclipse or a similar IDE, it should be easy enough.


  • Registered Users, Registered Users 2 Posts: 5,267 ✭✭✭Elessar


    I don't quite follow - what jar? I don't have any jar files at the moment.


  • Registered Users, Registered Users 2 Posts: 11,749 ✭✭✭✭wes


    Elessar wrote: »
    I don't quite follow - what jar? I don't have any jar files at the moment.

    You say your using Jasper Reports, so I assumed you downloaded the libraries from here:

    http://community.jaspersoft.com/project/jasperreports-library

    Direct link to the downloads:
    http://community.jaspersoft.com/project/jasperreports-library/releases

    So you will need the jar files you download there on your build path to use there libraries. Now, I assume that the libraries will let you access the report your generated. I don't know Jasper Reports, but there libraries should handle this, I would assume.


  • Registered Users, Registered Users 2 Posts: 5,267 ✭✭✭Elessar


    wes wrote: »
    You say your using Jasper Reports, so I assumed you downloaded the libraries from here:

    http://community.jaspersoft.com/project/jasperreports-library

    Direct link to the downloads:
    http://community.jaspersoft.com/project/jasperreports-library/releases

    So you will need the jar files you download there on your build path to use there libraries. Now, I assume that the libraries will let you access the report your generated. I don't know Jasper Reports, but there libraries should handle this, I would assume.

    Ah yes of course. But what do to next? How does one link an iReport to Java using JavaBeans?


  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    Have a look at the resources section here: http://community.jaspersoft.com/project/jasperreports-library/resources

    and the tutorial here: http://community.jaspersoft.com/wiki/jasperreports-library-tutorial

    The general gist is that you'll need to include the jasper reports jars on your build path/class path and then use the jasper reports API to take your data and fill it in to the template.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,267 ✭✭✭Elessar


    Ok thanks. After having a look over the documentation am I right in thinking the below are the correct steps to fill a PDF and export it:

    1. Import the jasperReport jar file into NetBeans classpath
    2. Design report in iReport
    3. Add database connection to fill report
    4. Compile report
    5. Load report jar file into classpath in NetBeans
    6. Use Jasper API to fill the report file and export to PDF

    I'm stuck on 3! This is all very new to me so a lot of head scratching going on :D

    I don't want to use a database as I obviously don't have one, so I assume I will have to use a Javabean to pass the methods/variables to the report? This is a list of available database connections:

    javabean_db_con.png


  • Registered Users, Registered Users 2 Posts: 5,267 ✭✭✭Elessar


    Ok so after much head scratching and frustration I finally figured it out. There are no definitive tutorials on how to use iReport with a JavaBean datasource so I'll lay out what I did here to help other developers who might be looking!

    First off I installed the iReport plugin for Netbeans - this didn't work correctly initially (no preview was visible) so a re-install of the java 7 RE and SDK sorted it.

    From my understanding a JavaBean is simply a collection of objects that you call from a library or plugin like iReport. First create a "bean" class (just a normal java class) with member variables you want visible in your report and getter/setter methods for each. The create a "factory" class (again name it anything) and have a static method in it that returns a collection:
    public static Collection getStudentList()
         {
             Vector students = new Vector();
             try
             {
                 Student student = new Student();
                 student.setRoll_no("101");
                 student.setName("Steve");
                 student.setFathers_name("Jack");
                 student.setStudying_in("I-A");
                 students.add(student);
                 student = new Student();
                 student.setRoll_no("102");
                 student.setName("Mark");
                 student.setFathers_name("Henry");
                 student.setStudying_in("I-A");
                 students.add(student);
             }
             catch(Exception ex)
             {
                 System.out.println(ex);
             }
             return students;
         }
    

    With Student being the "bean" class in this example. In Netbeans, go to Preferences>iReport>Classpath and set the class path to the .class files (of the code above) in your project or compile them into jars.

    Then set a new javabeans data source in Netbeans. Add a title and for the factory class, set your package name where the collection lies. i.e. com.myexample.Collection, select Collection of JavaBeans button, and add the name of the static method in it i.e. getStudentList. Test it and it should work.

    Now design a new report in iReport and click on the Report Query button. Type in the name of the "bean" class you created i.e. package.Student, read attributes and then you should see the variables you had added in your bean class. Add whatever fields you want and click ok. The variables are now available for you to drag into the PDF from "Fields" section of the Report Inspector.

    Hope that helps someone else! Now I just need to figure out how to call a method in the PDF instead of variables.


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    Just a quick FYI - I believe the iReports is deprecated by the Jaspersoft Studio.
    Now they're both just graphical ways toi create the jrxml files so both work for now... but there will be no new releases of iReport... or so I read!


Advertisement