Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package myjava;
- import java.sql.*;
- import java.util.*;
- import javax.swing.JOptionPane;
- import net.sf.jasperreports.engine.*;
- import net.sf.jasperreports.engine.design.JasperDesign;
- import net.sf.jasperreports.engine.xml.JRXmlLoader;
- import net.sf.jasperreports.view.*;
- public class PrintPreview2{
- public PrintPreview2(){
- try{
- //Load database driver and connect
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost/my3csd","root","12345"
- );
- //User Input
- String sn = JOptionPane.showInputDialog("Enter a Student Number");
- //Set parameters to be sent to the report
- Map param = new HashMap();
- param.put("studnum", sn);
- param.put("user", "Jess Agbayani");
- //Specify source and Destination of Report
- String sourceDir = "D:\\report2.jrxml";
- String destinationDir = "D:\\Jreport.pdf";
- //Load the report
- JasperDesign jd = JRXmlLoader.load(sourceDir);
- //Compile the report
- JasperReport jr = JasperCompileManager.compileReport(jd);
- //Fill report
- JasperPrint jp = JasperFillManager.fillReport(jr,param,conn);
- //Export report to E:\JReport.pdf
- JasperExportManager.exportReportToPdfFile(jp,destinationDir);
- //Preview Report
- JasperViewer.viewReport(jp,false); //DISPOSE_ON_CLOSE
- //close the connection
- conn.close();
- }
- catch (Exception e){
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- public static void main(String args[]){
- PrintPreview2 pv = new PrintPreview2();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement