java-mysql-jasperreports-netbeansI would like to share the code that I have found online and use to write a program that let me connect MySQL to a Java program, create and view a report using JasperReports library.

The following are codes that let you insert a new record on a table in MySQL. Make sure that “MySQL JDBC Driver” is added on the CLASS PATH or Libraries of your Java project which can be done via right clicking the Project name-> Properties->Libraries->Add Library or Add JAR/Folder.

JFrame frame;
try {

// setup the connection with the DB.
connect = DriverManager
+ "user=myuser&password=mypassword");

preparedStatement = connect
.prepareStatement("insert into jphonebook.contacts (jname, jcontact, jaddress) "
+ "values (?, ?, ?)");
preparedStatement.setString(1, txtName.getText());
preparedStatement.setString(2, txtNo.getText());
preparedStatement.setString(3, txtAddress.getText());
frame = new JFrame("F1");
JOptionPane.showMessageDialog(frame,"Saved."); // show msgb
} catch (Exception e) {
} finally {

private void close() {
try {
} catch (Exception e) {
// error

The next code shows how to call JasperReports in a viewer and specify a specific MySQL query to fill the report.

JasperPrint jasperPrint;
HashMap hm = null;
Connection connect = null;
try {
// setup the connection with the DB.
connect = DriverManager
+ "user=root&password=");
} catch (Exception ex) {
System.out.println("Database.getConnection() Error -->" + ex.getMessage());

/**You can also test this line if you want to display
* report from any absolute path other than the project root path*/

String ReportFile = "report1.jrxml";
String jasperFileName = "report1.jasper";

InputStream inputx = new FileInputStream(new File("report1.jrxml"));
try {
JasperDesign jasperDesign = JRXmlLoader.load(inputx);

JRDesignQuery jrd = new JRDesignQuery();

// You can change this query to specify what data to be display on the report.

String q = "SELECT * FROM jphonebook.contacts ORDER BY jname";


JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
jasperPrint = JasperFillManager.fillReport(jasperReport, null, connect);
//JRViewer viewer = new JRViewer(jasperPrint);

//OutputStream output = new FileOutputStream(new File("myreport.pdf"));
//JasperExportManager.exportReportToPdfStream(jasperPrint, output);

JasperViewer jv = new JasperViewer(jasperPrint);
jv.setTitle("List of Contacts");
//Container c = getContentPane();
//setTitle("Report Viewer");
} catch (JRException e) {

Make sure to import the following packages/libraries so that these codes will work.

//for the first code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.*;

//for the JasperReports code
import javax.swing.JFrame;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

Now if you hate this messy codes and want the complete working code, you can download the whole java package here.

If you have any questions, say thank you or anything to add on this, please feel free to add your comments below. More Java codes later guys, so happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.