// Create Connection Class.forName("com.mysql.jdbc.Driver"); connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" + "?user=root&password=root"); // Report Viewer String report = new File(".").getCanonicalPath()+ "\\myReport.jrxml"; JasperReport ir = JasperCompileManager.compileReport(report); JasperPrint ip = JasperFillManager.fillReport(ir, null,connect); JasperViewer.viewReport(ip); // Close Connection connect.close();
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.view.JasperViewer;
package com.java.myapp; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; 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.view.JasperViewer; public class MyForm extends JFrame { /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm frame = new MyForm(); frame.setVisible(true); } }); } /** * Create the frame. */ public MyForm() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 431, 286); setTitle("ThaiCreate.Com Java GUI Tutorial"); getContentPane().setLayout(null); // Button Report JButton btnOpenReport = new JButton("Open Report"); btnOpenReport.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Connection connect = null; try { Class.forName("com.mysql.jdbc.Driver"); connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" + "?user=root&password=root"); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Application path String report = null; try { report = new File(".").getCanonicalPath()+ "\\myReport.jrxml"; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { // Report Viewer JasperReport ir = JasperCompileManager.compileReport(report); JasperPrint ip = JasperFillManager.fillReport(ir, null,connect); JasperViewer.viewReport(ip); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connect.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btnOpenReport.setBounds(137, 98, 146, 23); getContentPane().add(btnOpenReport); setResizable (false); } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท