Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. package myjava;
  2.  
  3.  
  4.  
  5. import java.sql.*;
  6.  
  7. import java.util.*;
  8.  
  9. import javax.swing.JOptionPane;
  10.  
  11. import net.sf.jasperreports.engine.*;
  12.  
  13. import net.sf.jasperreports.engine.design.JasperDesign;
  14.  
  15. import net.sf.jasperreports.engine.xml.JRXmlLoader;
  16.  
  17. import net.sf.jasperreports.view.*;
  18.  
  19.  
  20.  
  21. public class PrintPreview2{
  22.  
  23.  
  24.  
  25. public PrintPreview2(){
  26.  
  27. try{
  28.  
  29. //Load database driver and connect
  30.  
  31. Class.forName("com.mysql.jdbc.Driver");
  32.  
  33. Connection conn = DriverManager.getConnection(
  34.  
  35. "jdbc:mysql://localhost/my3csd","root","12345"
  36.  
  37. );
  38.  
  39.  
  40.  
  41. //User Input
  42.  
  43. String sn = JOptionPane.showInputDialog("Enter a Student Number");
  44.  
  45. //Set parameters to be sent to the report
  46.  
  47. Map param = new HashMap();
  48.  
  49. param.put("studnum", sn);
  50.  
  51. param.put("user", "Jess Agbayani");
  52.  
  53.  
  54.  
  55. //Specify source and Destination of Report
  56.  
  57. String sourceDir = "D:\\report2.jrxml";
  58.  
  59. String destinationDir = "D:\\Jreport.pdf";
  60.  
  61.  
  62.  
  63. //Load the report
  64.  
  65. JasperDesign jd = JRXmlLoader.load(sourceDir);
  66.  
  67.  
  68.  
  69. //Compile the report
  70.  
  71. JasperReport jr = JasperCompileManager.compileReport(jd);
  72.  
  73.  
  74.  
  75. //Fill report
  76.  
  77. JasperPrint jp = JasperFillManager.fillReport(jr,param,conn);
  78.  
  79.  
  80.  
  81. //Export report to E:\JReport.pdf
  82.  
  83. JasperExportManager.exportReportToPdfFile(jp,destinationDir);
  84.  
  85.  
  86.  
  87. //Preview Report
  88.  
  89. JasperViewer.viewReport(jp,false); //DISPOSE_ON_CLOSE
  90.  
  91.  
  92.  
  93. //close the connection
  94.  
  95. conn.close();
  96.  
  97. }
  98.  
  99. catch (Exception e){
  100.  
  101. JOptionPane.showMessageDialog(null, e.getMessage());
  102.  
  103. }
  104.  
  105. }
  106.  
  107.  
  108.  
  109. public static void main(String args[]){
  110.  
  111. PrintPreview2 pv = new PrintPreview2();
  112.  
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement