AllTutorials
Java
Android
C#
C/C++
Database
Report
Client
Others
CallingJasperReportfromJavaApplicationtutorial
RelatedDocumentCategories
1
DownloadJasperReportlibraryforJava
DownloadJDBCDrivers
Preparereports
CreateaJavaprojectanddeclarelibrary
GeneratePDF
TheutilityclassestoconnectsomekindofDatabase
PassDatasourceandParameters
ReportProgramming
JavaBasic
WhatisneededtogetstartedwithJava?
1DownloadJasperReportlibraryforJava
GuidetoInstallingandConfiguringJava
GuidetoInstallingandConfiguringEclipse
QuickLearningJavaforbeginners
http://community.jaspersoft.com/download
JDKJavadocinCHMformat
InheritanceandpolymorphisminJava
AbstractclassandInterfaceinJava
AccessmodifiersinJava
JavaEnumTutorial
JavaAnnotationTutorial
ComparisonandsortinginJava
Java String, StringBuffer and StringBuilder
Tutorial
JavaExceptionHandlingTutorial
JavaGenericsTutorial
JavaCollectionFrameworkTutorial
JavaIOTutorialBinaryStreams
JavaIOTutorialCharacterStreams
JavaDateTimeTutorial
SyntaxandnewfeaturesinJava8
JavaRegularexpressionTutorial
JavaMultithreadingProgrammingTutorial
JDBC Driver Libraries for different types of
databaseinJava
JavaJDBCtutorial
JavaCompressionTutorial
JavareflectionTutorial
JavaremotemethodinvocationJavaRMI
Tutorial
JavaSocketProgrammingTutorial
Java Desktop Application Programming
usingSWT
EclipseJFaceTutorial
CommonsIOTutorial
CommonsLoggingTutorial
AdvancedJava
Java Programming for team using Eclipse
andSVN
ServersideProgramming
ConfiguringTomcatServerinEclipse
Guide to installing
GlassfishWebServer
and
configuring
JavaServletTutorialforBeginners
JavaServletFilterTutorial
JavaJSPTutorialforBeginners
InstallWebToolsPlatformintoEclipse
Create a Simple Web Application Using
Servlet,JSPandJDBC
Using Google reCAPTCHA with Java Web
Application
Struts2Framework
Struts2TutorialforBeginners
Result:
SpringFramework
SpringTutorialforBeginners
Spring MVC Tutorial for Beginners Hello
Spring4MVC
InstallSpringToolSuiteintoEclipse
Configuring Static Resource and Resource
BundleinSpringMVCTutorial
SpringMVCFileUploadTutorial
SpringJDBCTutorial
Simple Login Web Application using Spring
MVC,SpringSecurityandSpringJDBC
SpringMVCandVelocityTutorial
UsingTemplateinSpringMVCwithApache
Tiles
Spring MVC and Spring JDBC Transaction
Tutorial
Spring MVC and Hibernate Transaction
Tutorial
EclipseTechnology
Unzipthedownloadedfile:
Install AspectJ development tools into
Eclipse
JavaAspectOrientedProgrammingTutorial
withAspectJ(AOP)
How to get the open source Java libraries
asOSGi(s)
InstallTychointoEclipse
JavaOSGiTutorialforBeginners
OSGiandAspectJintegration
Building OSGi project with Maven and
tycho
InstallWindowBuilderintoEclipse
Java Desktop Application Programming
usingSWT
EclipseJFaceTutorial
Install e4 Tools Developer Resources into
Eclipse
Eclipse RCP 4 Tutorial for Beginners E4
WorkbenchApplication
Guide to Packing and Deploying Desktop
ApplicationSWT/RCP
InstallRAPtoolsintoEclipse
InstallRAPe4ToolingintoEclipse
InstallEclipseRAPTargetPlatform
Eclipse RAP Tutorial for Beginners Basic
Application
Eclipse RAP Tutorial for Beginners e4
WorkbenchApplication
Create Eclipse RAP Widget
ClientScriptingbasedwidget
from
2DownloadJDBCDrivers
Ifthereportconnectedtoadatabase,youneedtohavetheJDBCdrivers:
JavaAPIforHTML&XML
ParsinganXMLFileUsingSAX
JDOM2Tutorial
JAXBTutorial
JavaXStreamTutorial
JsoupJavaHtmlParserTutorial
JavaOpensourcelibraries
SkypeJavaAPITutorial
Using Scribe OAuth Java API with Google
OAuth2Tutorial
JDOM2Tutorial
JavaXStreamTutorial
YoucanseetheguidetodownloadJDBCDriverat:
http://o7planning.org/web/fe/default/en/document/21135/jdbcdriverlibrariesfordifferenttypesof
databaseinjava
3Preparereports
YouneedaJasperReportfile.WewillrunthatreportonJava.Youcandownloadanexampleofa
reportfileat:
JsoupJavaHtmlParserTutorial
RetrievingGeographicinformationbasedon
IPAddressusingGeoIP2JavaAPI
NewestDocuments
XMLParserforOraclePL/SQL
CreatingandManagingOracleWallet
https://community.jaspersoft.com/sites/default/files/files/StyledTextReport.zip
Unzipthedownloadedfile,wewillhavejrxmlfile.
AndroidFragmentsTutorial
Guide to Installing Intel Hardware
Accelerated Execution Manager (Intel
HAXM)
AndroidGridViewTutorial
Install C/C++ Development Tooling into
Eclipse
GuidetoInstallingC/C++CompilerCygWin
GuidetoInstallingC/C++CompilerMinGW
Guide to Installing and Configuring Visual
Studio2013
JavaXStreamTutorial
QuickLink
4CreateaJavaprojectanddeclarelibrary
CreateJavaProject:
JavaCallJasperReportTutorial
DownloadJasperReportlibraryforJava
DownloadJDBCDrivers
Preparereports
CreateaJavaprojectanddeclarelibrary
GeneratePDF
Theutilityclassestoconnectsomekindof
Database
7
Createalibsfolderandcopythemostvitallibrariesintothisfolder,including:
PassDatasourceandParameters
commonsbeanutils*.jar
commonscollections*.jar
commonsdigester*.jar
commonslogging*.jar
groovyall*.jar
itextpdf*.jar
itextpdfa*.jar
jasperreports*.jar
Next,copyDatabaseDriverintolibsfolder.HereIcopyallofthreeJDBCDriverintoDatabasetypes
includingOracle,MySQL,SQLServer.
Todeclarethelibrary,rightclickProjectandselectProperties
Selectallthejarfilesinthelibsfolder:
5GeneratePDF
Hereisasimpleexample,JavawillcalltoreportJasperReportandgeneratePDFfile.Thissimple
exampledoesnothavetheparticipationoftheDatabase.
PdfFromXmlFile.java
1
2
packageorg.o7planning.tutorial.javajasperreport;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
importjava.io.File;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
importnet.sf.jasperreports.engine.JRDataSource;
importnet.sf.jasperreports.engine.JREmptyDataSource;
importnet.sf.jasperreports.engine.JRException;
importnet.sf.jasperreports.engine.JasperCompileManager;
importnet.sf.jasperreports.engine.JasperExportManager;
importnet.sf.jasperreports.engine.JasperFillManager;
importnet.sf.jasperreports.engine.JasperPrint;
importnet.sf.jasperreports.engine.JasperReport;
publicclassPdfFromXmlFile{
publicstaticvoidmain(String[]args)throwsJRException,IOException{
//Compilejrxmlfile.
JasperReportjasperReport=JasperCompileManager
.compileReport("C:/jasperreport/StyledTextReport/StyledTextReport.jrxml"
//Parametersforreport
Map<String,Object>parameters=newHashMap<String,Object>();
//DataSource
//Thisissimpleexample,nodatabase.
//thenusingemptydatasource.
JRDataSourcedataSource=newJREmptyDataSource();
JasperPrintjasperPrint=JasperFillManager.fillReport(jasperReport,
parameters,dataSource);
//Makesuretheoutputdirectoryexists.
FileoutDir=newFile("C:/jasperoutput");
outDir.mkdirs();
//ExporttoPDF.
JasperExportManager.exportReportToPdfFile(jasperPrint,
"C:/jasperoutput/StyledTextReport.pdf");
System.out.println("Done!");
46
46
47
}
}
Resultsofrunningtheexample:
6TheutilityclassestoconnectsomekindofDatabase
Oracle
MySQL
SQLServer
OracleConnUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
?
packageorg.o7planning.tutorial.javajasperreport.conn;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
publicclassOracleConnUtils{
publicstaticConnectiongetOracleConnection()
throwsClassNotFoundException,SQLException{
StringhostName="localhost";
Stringsid="db11g";
StringuserName="learningsql";
Stringpassword="12345";
returngetOracleConnection(hostName,sid,userName,password);
}
publicstaticConnectiongetOracleConnection(StringhostName,Stringsid,
StringuserName,Stringpassword)throwsClassNotFoundException,
SQLException{
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
SQLException{
//DeclaretheclassDriverforORACLEDB
//ThisisnecessarywithJava5(orolder)
//Java6(ornewer)automaticallyfindtheappropriatedriver.
//IfyouuseJava>5,thenthislineisnotneeded.
Class.forName("oracle.jdbc.driver.OracleDriver");
//Example:jdbc:oracle:thin:@localhost:1521:db11g
StringconnectionURL="jdbc:oracle:thin:@"+hostName+":1521:"+sid;
Connectionconn=DriverManager.getConnection(connectionURL,userName,
password);
returnconn;
}
}
MySQLConnUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
?
packageorg.o7planning.tutorial.javajasperreport.conn;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
publicclassMySQLConnUtils{
publicstaticConnectiongetMySQLConnection()
throwsClassNotFoundException,SQLException{
StringhostName="localhost";
StringdbName="learningsql";
StringuserName="root";
Stringpassword="12345";
returngetMySQLConnection(hostName,dbName,userName,password);
}
publicstaticConnectiongetMySQLConnection(StringhostName,StringdbName,
19
StringuserName,Stringpassword)throwsSQLException,
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
StringuserName,Stringpassword)throwsSQLException,
ClassNotFoundException{
//DeclaretheclassDriverforMySQLDB
//ThisisnecessarywithJava5(orolder)
//Java6(ornewer)automaticallyfindtheappropriatedriver.
//IfyouuseJava>5,thenthislineisnotneeded.
Class.forName("com.mysql.jdbc.Driver");
//CutrcURLConnectiondnhchoOracle
//Vd:jdbc:mysql://localhost:3306/simplehr
StringconnectionURL="jdbc:mysql://"+hostName+":3306/"+dbName;
Connectionconn=DriverManager.getConnection(connectionURL,userName,
password);
returnconn;
}
}
SQLServerConnUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
packageorg.o7planning.tutorial.javajasperreport.conn;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
publicclassSQLServerConnUtils{
//ConnecttoSQLServer
//(UsingJDBCDriver:SQLJDBC)
publicstaticConnectiongetSQLServerConnection_SQLJDBC()
throwsClassNotFoundException,SQLException{
StringhostName="localhost";
StringsqlInstanceName="SQLEXPRESS";
Stringdatabase="learningsql";
StringuserName="sa";
Stringpassword="12345";
19
returngetSQLServerConnection_SQLJDBC(hostName,sqlInstanceName,
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
returngetSQLServerConnection_SQLJDBC(hostName,sqlInstanceName,
database,userName,password);
}
//ConnecttoSQLServer&usingJTDSlibrary
publicstaticConnectiongetSQLServerConnection_JTDS()throwsSQLException,
ClassNotFoundException{
StringhostName="learningsql";
StringsqlInstanceName="SQLEXPRESS";
Stringdatabase="simplehr";
StringuserName="sa";
Stringpassword="12345";
returngetSQLServerConnection_JTDS(hostName,sqlInstanceName,database,
userName,password);
}
//ConnecttoSQLServer
//(UsingJDBCDriver:SQLJDBC)
privatestaticConnectiongetSQLServerConnection_SQLJDBC(StringhostName,
StringsqlInstanceName,Stringdatabase,StringuserName,
Stringpassword)throwsClassNotFoundException,SQLException{
//DeclaretheclassDriverforSQLServerDB
//ThisisnecessarywithJava5(orolder)
//Java6(ornewer)automaticallyfindtheappropriatedriver.
//IfyouuseJava>5,thenthislineisnotneeded.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//jdbc:sqlserver://ServerIp:1433/SQLEXPRESS;databaseName=simplehr
StringconnectionURL="jdbc:sqlserver://"+hostName+":1433"
+";instance="+sqlInstanceName+";databaseName="+database;
Connectionconn=DriverManager.getConnection(connectionURL,userName,
password);
returnconn;
}
//ConnecttoSQLServer&usingJTDSlibrary
privatestaticConnectiongetSQLServerConnection_JTDS(StringhostName,
StringsqlInstanceName,Stringdatabase,StringuserName,
Stringpassword)throwsClassNotFoundException,SQLException{
63
//DeclaretheclassDriverforSQLServerDB
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//DeclaretheclassDriverforSQLServerDB
//ThisisnecessarywithJava5(orolder)
//Java6(ornewer)automaticallyfindtheappropriatedriver.
//IfyouuseJava>5,thenthislineisnotneeded.
Class.forName("net.sourceforge.jtds.jdbc.Driver");
//jdbc:jtds:sqlserver://localhost:1433/simplehr;instance=SQLEXPRESS
StringconnectionURL="jdbc:jtds:sqlserver://"+hostName+":1433/"
+database+";instance="+sqlInstanceName;
Connectionconn=DriverManager.getConnection(connectionURL,userName,
password);
returnconn;
}
ConnectionUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
packageorg.o7planning.tutorial.javajasperreport.conn;
importjava.sql.Connection;
importjava.sql.SQLException;
publicclassConnectionUtils{
publicstaticConnectiongetConnection()throwsSQLException,
ClassNotFoundException{
//UsingOracle
//YoumaybereplacedbyotherDatabase.
returnOracleConnUtils.getOracleConnection();
}
//
//TestConnection...
//
publicstaticvoidmain(String[]args)throwsSQLException,
ClassNotFoundException{
21
21
22
23
24
25
26
27
28
29
30
31
System.out.println("Getconnection...");
//GetaConnectionobject
Connectionconn=ConnectionUtils.getConnection();
System.out.println("Getconnection"+conn);
System.out.println("Done!");
}
}
7PassDatasourceandParameters
Thefollowingexamplewillbemorecomplexthanthepreviousexample.Wewillpassparametersand
databaseconnectionintoreportfromJava.
JavaCallJasperReport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
packageorg.o7planning.tutorial.javajasperreport;
importjava.io.File;
importjava.sql.Connection;
importjava.sql.SQLException;
importjava.util.HashMap;
importjava.util.Map;
importnet.sf.jasperreports.engine.JRException;
importnet.sf.jasperreports.engine.JasperCompileManager;
importnet.sf.jasperreports.engine.JasperFillManager;
importnet.sf.jasperreports.engine.JasperPrint;
importnet.sf.jasperreports.engine.JasperReport;
importnet.sf.jasperreports.engine.export.JRPdfExporter;
importnet.sf.jasperreports.export.ExporterInput;
importnet.sf.jasperreports.export.OutputStreamExporterOutput;
importnet.sf.jasperreports.export.SimpleExporterInput;
importnet.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
importnet.sf.jasperreports.export.SimplePdfExporterConfiguration;
20
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
importorg.o7planning.tutorial.javajasperreport.conn.ConnectionUtils;
publicclassJavaCallJasperReport{
publicstaticvoidmain(String[]args)throwsJRException,
ClassNotFoundException,SQLException{
StringreportSrcFile="F:/ECLIPSE_TUTORIAL/JASPERREPORT/HelloJasperReport/FirstJasperReport.jrxml"
//First,compilejrxmlfile.
JasperReportjasperReport=JasperCompileManager.compileReport(reportSrcFile);
Connectionconn=ConnectionUtils.getConnection();
//Parametersforreport
Map<String,Object>parameters=newHashMap<String,Object>();
JasperPrintprint=JasperFillManager.fillReport(jasperReport,
parameters,conn);
//Makesuretheoutputdirectoryexists.
FileoutDir=newFile("C:/jasperoutput");
outDir.mkdirs();
//PDFExportor.
JRPdfExporterexporter=newJRPdfExporter();
ExporterInputexporterInput=newSimpleExporterInput(print);
//ExporterInput
exporter.setExporterInput(exporterInput);
//ExporterOutput
OutputStreamExporterOutputexporterOutput=newSimpleOutputStreamExporterOutput(
"C:/jasperoutput/FirstJasperReport.pdf");
//Output
exporter.setExporterOutput(exporterOutput);
//
SimplePdfExporterConfigurationconfiguration=newSimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
System.out.print("Done!");
64
64
65
}
}
o7planning.org