lunes, 25 de abril de 2011

IReport and JAVA



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* getGenerarReport.java
*
* Created on 16/04/2011, 05:45:41 PM
* by: Añez Juan
*/

package Report;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
*
* @author root
*/
public class getGenerarReport extends javax.swing.JFrame {
private Connection connections;
private Statement statements;
private ResultSet resulset;

private static String ip = "localhost";
private static String data_base = "jdb_rieb";
private static String user = "root";
private static String password = "admin18772";

/** Creates new form getGenerarReport */
public getGenerarReport() {
initComponents();
mysqlConect();
setLocationRelativeTo(null);
setTitle("");
}

/** 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.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("USUARIOS");
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextField1.setFont(new java.awt.Font("Dialog", 0, 48));
jTextField1.setName("jTextField1"); // NOI18N

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE))
);

pack();
}//


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new getGenerarReport().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

private void mysqlConect(){
try {
Class.forName("com.mysql.jdbc.Driver");
connections = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/"+data_base,user,password);
} catch (SQLException ex) {
Logger.getLogger(getGenerarReport.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(getGenerarReport.class.getName()).log(Level.SEVERE, null, ex);
}
}
@SuppressWarnings("unchecked")
public void getGeneraReporteUsuario(){
mysqlConect();
try {
String archivojasper = ("src/Report/Usuarios.jasper");
if (archivojasper== null) {
JOptionPane.showMessageDialog(null, "El archivo " + archivojasper + " no se " + "encuentra en la carpeta del sistema");
} //end _if
JasperReport masterreport=null;
try {
masterreport = (JasperReport) JRLoader.loadObject(archivojasper);
} catch (Exception e) {

JOptionPane.showMessageDialog(null, "El error se debe a "+e.getLocalizedMessage() +"\n"+ e.getCause());
System.exit(0);
}
//solo_si_se_Decea_pasa_parametros
//HashMap parametros = new HashMap();
//parametros.put("pass", jTextField1.getText());
JasperPrint jasperPrint = JasperFillManager.fillReport(masterreport, null, connections);

JasperViewer viewer = new JasperViewer(jasperPrint, false);
viewer.setTitle("Usuarios - RIEB");
viewer.setVisible(true);
} catch (JRException ex) {
Logger.getLogger(getGenerarReport.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}

}
@SuppressWarnings("unchecked")
public void getGenerarReporteDespachos(){
mysqlConect();
try {
String archivojasperd = ("src/Report/Despachos.jasper");
if (archivojasperd== null) {
JOptionPane.showMessageDialog(null, "El archivo " + archivojasperd + " no se " + "encuentra en la carpeta del sistema");
} //end _if
JasperReport masterreportd=null;
try {
masterreportd = (JasperReport) JRLoader.loadObject(archivojasperd);
} catch (Exception ed) {

JOptionPane.showMessageDialog(null, "El error se debe a "+ed.getLocalizedMessage() +"\n"+ ed.getCause());
System.exit(0);
}
//solo_si_se_Decea_pasa_parametros
//HashMap parametros = new HashMap();
//parametros.put("pass", jTextField1.getText());
JasperPrint jasperPrintd = JasperFillManager.fillReport(masterreportd, null, connections);

JasperViewer viewerd = new JasperViewer(jasperPrintd, false);
viewerd.setTitle("Despachos - RIEB");
viewerd.setLocationRelativeTo(null);
viewerd.setExtendedState(MAXIMIZED_BOTH);
viewerd.setVisible(true);
} catch (JRException exd) {
Logger.getLogger(getGenerarReport.class.getName()).log(Level.SEVERE, null, exd);
exd.printStackTrace();
}
}
}