1 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
import java.awt.BorderLayout;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
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.xml.JRXmlLoader;
import net.sf.jasperreports.view.JRViewer;
public class JasperViewer extends javax.swing.JFrame {
private JRViewer viewer = null;
private boolean isExitOnClose = true;
/** Creates new form JasperViewer */
public JasperViewer(JasperPrint jasperPrint) {
this.isExitOnClose = true;
initComponents();
this.viewer = new JRViewer(jasperPrint);
this.pnlMain.add(this.viewer, BorderLayout.CENTER);
}
/** Creates new form JasperViewer */
public JasperViewer(JasperPrint jasperPrint, boolean isExitOnClose) {
this.isExitOnClose = isExitOnClose;
initComponents();
this.viewer = new JRViewer(jasperPrint);
this.pnlMain.add(this.viewer, BorderLayout.CENTER);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() { //GEN-BEGIN:initComponents
pnlMain = new javax.swing.JPanel();
setTitle("JasperViewer");
setIconImage(
new javax
.swing
.ImageIcon(
getClass().getResource(
"/net/sf/jasperreports/view/images/jricon.GIF"))
.getImage());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm();
}
});
pnlMain.setLayout(new java.awt.BorderLayout());
getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);
pack();
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new java.awt.Dimension(750, 550));
setLocation(
(screenSize.width - 750) / 2,
(screenSize.height - 550) / 2);
} //GEN-END:initComponents
/** Exit the Application */
void exitForm() { //GEN-FIRST:event_exitForm
if (this.isExitOnClose) {
System.exit(0);
} else {
this.setVisible(false);
this.viewer.clear();
this.viewer = null;
this.getContentPane().removeAll();
this.dispose();
}
} //GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
String fileName = null;
String url = "jdbc:as400://maMachine";
String login = "xxxxx";
String password = "yyyyy";
Connection connection = null;
try {
// - Connection to DB
Driver myDriver = new com.ibm.as400.access.AS400JDBCDriver();
DriverManager.registerDriver(myDriver);
connection = DriverManager.getConnection(url, login, password);
System.out.println("Connection is OK");
// - Load & compile report
JasperDesign jasperDesign = new JasperDesign();
jasperDesign = JRXmlLoader.load("C:\\Program Files\\JasperSoft\\iReport-2.0.4\\jrxml\\TEST1.jrxml");
System.out.println("Load is successful");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Compile is OK");
// - Paramètres à envoyer au rapport
Map parameters = new HashMap();
parameters.put("Titre", "All customers");
System.out.println("Put param is OK");
// - Execution du rapport
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
System.out.println("Fill report is OK");
// - Création du rapport au format PDF
viewReport(jasperPrint);
} catch (JRException e) {
System.out.println("JRException");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("SQLException");
e.printStackTrace();
} finally {
try {
connection.close();
//System.out.println("Connection is closed");
} catch (SQLException e) {
System.out.println("SQLException: close connection");
e.printStackTrace();
}
}
}
/**
*
*/
private static void usage() {
System.out.println("JasperViewer usage:");
System.out.println("\tjava JasperViewer -XML -Ffile");
}
/**
*
*/
public static void viewReport(JasperPrint jasperPrint) {
JasperViewer jasperViewer = new JasperViewer(jasperPrint, true);
jasperViewer.setVisible(true);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel pnlMain;
// End of variables declaration//GEN-END:variables
} |
Partager