You are on page 1of 8

JAPROFT.

COM UPPORT COMMUNITY CONTACT LOGIN/RGITR

PRODUCTS SOLUTIONS SERVICES RESOURCES

DownloadNow

Products Answers Exchange Docs Wiki Planet Tracker Search

HomeWikiSubreportsinJaspersoftStudio

CreateNewWikiPage
ureportsinJaspersofttudio
PageStatus NeedsCopy/StyleReview

Overview PageType GettingStarted

Audience BusinessUser
Thetutorial"CreatingChartsandSubreportswithJaspersoftStudio"providedabrief
TableofContents[hide]
introductiontothesubreportelement,whichallowsyoutoinsertareportinsideanother Overview Project JaspersoftStudio
report.Thispowerfultooallowsyoutocreatecomplexlayoutswithinasingledocument, CreationoftheDataAdapter
usingmultipledatasourcesandreports. CreationoftheMainReport Version v1.3.2
CreationoftheFirstSubreport
Inthistutorialwewanttocreateanaddressbooklisitngeachpersoninacompany,
CreationoftheSecondSubreport
includingtheirnames,phonenumbers,andemailaddresses,asshowninthefollowing 124usersfoundthishelpful.
CompletingtheMainReport
image:

Tags
JaspersoftStudio Subreports

Datasets JDBC Tutorials

Examples DataSources

DataAdapter

Todothis,weneedamainreportcontainingtwosubreports.

CreationoftheDataAdapter
Forthistask,we'llneedadatasourcemorecomplexthanthesampleDB.Forthisreason,weneedtoinstallJasperReportsServerwhichcontainssomeuseful
sampledatabases.TodeterminewhichversionofJaspersoftServeryoushouldusewithyouroperatingsystem,checkthetutorialGettingStartedwithJaspersoft
Studio,andlookinthesection"SoftwareRequirementsandInstallation".AfteryouhaveinstalledJaspersoftStudio,useittocreateanewJasperReportusing
thetemplate"FlowerGray"withthereportname"ContactsList".Whenaskedtoselectadataadapter,clickNew,thenselectDatabaseJDBCConnectionand
clickNext.

Whenaskedtochooseanameforyournewdataadapter,use"ContactsDataset"andconfigureitwiththisdata:

JBDCDriver:org.postgresql.Driver
JDBCURL:jdbc:postgresql://localhost:5432/sugarcrm
Username:postgres
Password:postgres

Afterthis,usethebutton"Test"tocheckiftheconnectionworks,andinthatcasehit"Finish".
CreationoftheMainReport
NowyoushouldbebackintheReportCreationwizard,atthestepwhereyouareaskedtoselectthedataadapter.Thenewdataadaptershouldbeselected
automaticallyifitisn't,selectContactsAdapter.IntheInsertanSQLquerytextentrybox,enterselectid,name,shipping_address_cityfromaccounts.This
queryreturnsthenameofacompanyandthecitywherethestoreislocated.ThenclickNext.

Asthenextstep,youareaskedtodefinegroups.Becausewedon'tneedthemforthistutorial,click>>toaddallthefieldstothereportandclickNext.
ClickNextagain,thenFinish.Thereportiscreated.Itshouldbesimilartotheoneintheimagebelow:
CreationoftheFirstureport
Thenextstepistocreateasubreporttodisplayemailaddresses.StartanewreportbyselectingFile>New>JasperReport.ChoosethetemplateBlankA4,
saveitinthesamedirectoryofthemasterreport,andcallit"EmailReport.jrxml".Whenaskedtochoosethedataadapter,selecttheContactsAdaptercreated
earlier,andusethefollowingquery:

selectcontacts.email1
fromcontacts,accounts_contacts
whereaccounts_contacts.account_id='placeholder'andaccounts_contacts.contact_id=contacts.id

Inthisquery,astaticstring"placeholder"isused.Assuggestedbythename,thisisnotarealvaluebutaplaceholderthatchangeswithaparameterdescribedin
thenextsteps.Afterenteringthequery,clickNext.

Thequerylocatesonefield.Addthatfieldtothereportbyclicking>>,thenclickNext.SkipthenextstepbyclickingNext,thenclickFinishtocreatethereport.

IntheOutlineview,selectthereportrootnode.Then,inthePropertiestab,clickEditPageFormat.
InthePageFormatwindow,reducethewidthofthepageto270pixelsandremovethemargins,sincetheyarenotusefulinasubreport,thenclickOK.

Becauseweusedastaticvalueasaplaceholder,thisreportdoesnotlistanyemailaddresses.Weneedtoretrievejustinformationforthecurrentpersoninthe
masterlist.Tofiltertheemailaddresses,weneedtouseaparameter.IntheOutlineview,selectthereportrootnode.TheninthePropertytab,ClickEditquery,
filterandsortoptions.IntheDataset&QueryDialogwindow,selecttheParameterstabandclicktheAddbutton.

Anewparameterisaddedwiththename"Parameter_1".Changethisnameto"ACCOUNT_ID",andmakesurethetypeStringisapplied.
Next,replacethestringplaceholder'withthevalue$P{ACCOUNT_ID}.Thisqueryisinfluencedbyaparameterthatcomesfromthemainreport.ClickOk.

Thesyntax$P{ACCOUNT_ID}allowstheuseofaparameterinsideaquery,inthiscasetofiltertheresultsusinga"where"condition.

Thenextstepistodragthebandcolumnheaderintothereport.AddaStaticTextelementfromthepaletteandchangeitto"eMailaddress".Dragthefield
email1fromtheOutlineviewintotheDetailband,thendeletetheunusedbands(title,pageheader,columnfooter,pagefooter,summary)andresizetheColumn
HeaderandDetailbands.Youshouldhavealayoutliketheoneintheimagebelow:

Savethereportand,ifyoulike,youcanrunittoseetheresult.

Creationoftheecondureport
Theproceduretocreatethesecondsubreportitisalmostidenticaltothefirstone.StartanewreportfromFile>New>JasperReport.ChooseBlankA4forthe
template,saveitinthesamedirectoryasthemaster,andcallitPhoneReport.jrxml.Select"ContactsAdapter"asthedataadapter,andenterthefollowingquery:

selectphone_work
fromcontacts,accounts_contacts
whereaccounts_contacts.account_id='placeholder'andaccounts_contacts.contact_id=contacts.id

Addthediscoveredfieldtothereport,don'tdefinegroupsandcompletethewizard.Whenthereportiscreated,clickEditPageFormat,removethemargins,and
setthewidthto270pixels.IntheEditquery,filterandsortexpressiondialog,switchtothetabparametersandaddanewparameter"ACCOUNT_ID".Usethis
toreplacetheplaceholderinthequery,asintheimagebelow:
Next,addaStaticTextelementcontainingthetext"Telephone"intheColumnHeaderband,andaddthefield"phone_work"totheDetailband.Deletethe
unusedbandsandresizethesetwoelementsandtheirbandsuntilyougetareportsimilartotheoneshownbelow:

Now,savethereportand,ifyouwant,compileittoseehowitlooks.

Nowit'stimetofinishtheworkonthemainreport.

CompletingtheMainReport
Thetwosubreportshavebeencreated.Now,weneedtoincludetheminthemainreport.

OpenthereportContactListandresizetheDetailbandsoitislargeenoughtoincludethesubreportsstartwith50pixels.Next,placeasubreportelementfrom
thePaletteintotheDetailbandtolaunchthewizard.ChooseSelectanexistingreport(selectedbydefault)andclicktheSelectareportfilebutton.Awindow
listingallcreatedreportsappears.SelectEmailReport.jrxml,clickOk,thenclickNext.
Inthenextstep,choosetheoptionUsethesameconnectionusedtofillthemasterreport(whichshouldbeselectedbydefaut).Thisensuresthedatabase
connectionispassedtothesubreport,soitcanexecuteitsSQLquery,thenclickNext.

Nowwecansetanexpressionfortheparameterexposedbythesubreport.ClickAddtoaddanewparameter.Doubleclicktoselectonthenewparameter's
name,andenter"ACCOUNT_ID".Selectthevaluefield,andandclickthebuttonwiththreedots(thebuttonisvisibleonlyifthevaluefieldisselected)toopenthe
ExpressionEditor.IntheExpressionEditor,doubleclicktoselectIDfromFields,thenclickFinish.Whenyoureturntotheparametersdialogitshouldlooksimilar
totheoneintheimagebelow:

NowclickFinish.Thefirstsubreportisaddedtothemainreport.

FromthePalettedraganothersubreportandrepeatthesameprocedureusingasreportfilePhoneReport.jrxml,andgivingalltheotherfieldsthesamevalues.
Atthispointyoushouldhavethetwosubreportsintothemainreport.Resizethemtoandresizethedetailtoobtainthisresult:
GointhePreviewtabtocompile.Ifeverythingiscorrect,youshouldseearesultsimilartotheonebelow.(Don'tworryiftheemailsareallthesame,thisisstill
correcttheyarethesameinthedatabaseweused.):

Answers UserGroups JasperReportserver


xchange ContriutetoourWiki JasperReportsLirar
Documentation RequestaFeature JaspersoftTL
Wiki Reportaug Jaspersofttudio
Tracker hareourxtension Visualize.js
ContactUs
Copright2017
TICOoftwareInc. PrivacPolic TermsofUse Trademarks
Allrightsreserved.

You might also like