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 {
Class.forName("com.mysql.jdbc.Driver");

// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/jphonebook?"
+ "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());
preparedStatement.executeUpdate();
frame = new JFrame("F1");
JOptionPane.showMessageDialog(frame,"Saved."); // show msgb
} catch (Exception e) {
System.out.println(e);
} finally {
close();
}

private void close() {
try {
connect.close();
} 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;
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
try {
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/jphonebook?"
+ "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";

jrd.setText(q);
jasperDesign.setQuery(jrd);

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.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jv.setExtendedState(MAXIMIZED_BOTH);
jv.setTitle("List of Contacts");
jv.setVisible(true);
//Container c = getContentPane();
//c.add(viewer);
//setTitle("Report Viewer");
//this.setVisible(true);
} catch (JRException e) {
e.printStackTrace();
}

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.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
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.design.JasperDesign;
import net.sf.jasperreports.engine.design.JRDesignQuery;
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!

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments