Sie sind auf Seite 1von 4

package paquete;

import java.awt.EventQueue;

public class ReportMain {

private static ConnectionDB con;

public static void main(String[] args) {


conectToDatabase();
openReportFrame();
}

private static void conectToDatabase() {


String driver="org.gjt.mm.mysql.Driver";
String url="jdbc:mysql://localhost";
String db="prueba";

con =new ConnectionDB(driver,db,url);


con.connect();
}

private static void openReportFrame() {


EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
try {
ReportFrame frame=new
ReportFrame(con.getConn());
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}

}
});
}

package paquete;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class ReportFrame extends JFrame {

public ReportFrame(Connection conn) {

AbstractJasperReports.createReport(conn,"C:\\Users\\undertaker\\Jaspersof
tWorkspace\\PruebaDB\\prueba.jasper");
initComponents();
}

private void initComponents() {


// TODO Auto-generated method stub
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 370, 164);
getContentPane().setLayout(null);
((JPanel) getContentPane()).setBorder(new EmptyBorder(5,5,5,5));

JButton btnGenerar=new JButton("Generar");


btnGenerar.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
AbstractJasperReports.showViewer();

}
});
btnGenerar.setBounds(44, 31, 110, 46);
getContentPane().add(btnGenerar);

JButton btnExportarPdf=new JButton("Exportar PDF");


btnExportarPdf.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

AbstractJasperReports.exportToPDF("D:\\nuevo]]report.pdf");

}
});
btnExportarPdf.setBounds(197, 31, 110, 46);
getContentPane().add(btnExportarPdf);
}

package paquete;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class ConnectionDB {


private String driver;
private String db;
private String url;

private Connection conn=null;


//----------------------------------------------

public ConnectionDB(String driver, String db, String url) {


this.driver = driver;
this.db = db;
this.url = url;
}

public void connect() {


try {
Class.forName(this.driver);
conn=DriverManager.getConnection(this.url+"/"+this.db,
"root", "kevinNIVEK");
} catch (ClassNotFoundException e) {
e.printStackTrace();

}catch(SQLException e) {
e.printStackTrace();
}
}

public void disconnect() {


try {
conn.close();
conn=null;
} catch (Exception e) {
e.printStackTrace();
}
}

public void getStateConnection() {


try {
if (!this.conn.isClosed()) {
JOptionPane.showMessageDialog(null, "conectado");
} else {
JOptionPane.showMessageDialog(null, "desconectado");
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public Connection getConn() {


return this.conn;
}
}
package paquete;

import java.sql.Connection;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JRException;

public abstract class AbstractJasperReports {


private static JasperReport report;
private static JasperPrint reportFilled;
private static JasperViewer viewer;

public static void createReport(Connection conn,String path) {

try {
report=(JasperReport) JRLoader.loadObjectFromFile(path);
reportFilled=JasperFillManager.fillReport(report,null,
conn);
} catch (JRException e) {
// TODO: handle exception
e.printStackTrace();
}
}

public static void showViewer() {


viewer =new JasperViewer(reportFilled);
viewer.setVisible(true);

public static void exportToPDF(String destination) {


try {

JasperExportManager.exportReportToPdfFile(reportFilled,destination);
} catch (JRException e) {
e.printStackTrace();
}