Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Problema con jasper reoport
Forum - Java - Problema con jasper reoport

Avatar
Bonny (Member)
Expert


Messaggi: 437
Iscritto: 24/04/2009

Segnala al moderatore
Postato alle 12:01
Giovedì, 13/09/2012
Salve ragazzi sto provando la libreria jasper report per creare dei pdf con una semplice applicazione di esempio
Questa è la classe di esempio:
Codice sorgente - presumibilmente Java

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import net.sf.jasperreports.engine.JRException;
  12. import net.sf.jasperreports.engine.JasperCompileManager;
  13. import net.sf.jasperreports.engine.JasperExportManager;
  14. import net.sf.jasperreports.engine.JasperFillManager;
  15.  
  16.  
  17. /**
  18.  * @author Bonny
  19.  *
  20.  */
  21. public class ReportTest {
  22.  
  23.    private Connection conn = null;
  24.    private String jrxml = "ReportTest.jrxml";
  25.  
  26.    public ReportTest() {
  27.  
  28.       long start = System.currentTimeMillis();
  29.       // compilazione del report (creazione del .jasper a partire dal .jrxml)
  30.       try {
  31.          JasperCompileManager.compileReportToFile(jrxml);
  32.       } catch (Exception jre) {
  33.          System.err.println("GeneralTest.createReport(): eccezione: " + jre.getMessage());
  34.       }
  35.       System.out.println("Compile time : " + (System.currentTimeMillis() - start));
  36.  
  37.       Map parametri = new HashMap();
  38.  
  39.       parametri.put("ReportTitle", "LucaBonaldo Report Example");
  40.       // parametri.put( "FilterClause", "" );
  41.       // parametri.put( "OrderClause", "" );
  42.       try {
  43.          conn = this.getConnection();
  44.          this.createDB(conn);
  45.          // creazione del report (file .jrprint)
  46.          JasperFillManager.fillReportToFile("file.jrprint", parametri, conn);
  47.          // export del report in PDF
  48.          JasperExportManager.exportReportToPdfFile("file.jrprint", "report.pdf");
  49.          System.out.println("GeneralTest.createReport(): PDF running time : " + (System.currentTimeMillis() - start));
  50.          conn.close();
  51.       } catch (ClassNotFoundException | SQLException | JRException ex) {
  52.          System.err.println("Connessione a sqlite non riuscita\nErrore:" + ex.getMessage());
  53.       }
  54.    }
  55.  
  56.    private Connection getConnection() throws ClassNotFoundException, SQLException {
  57.  
  58.       String driver = "org.sqlite.JDBC";
  59.       String connectString = "jdbc:odbc:jasper.db";
  60.       String user = "";
  61.       String psw = "";
  62.  
  63.       Class.forName(driver);
  64.       Connection c = DriverManager.getConnection(connectString, user, psw);
  65.  
  66.       return c;
  67.    }
  68.  
  69.    private void createDB(Connection c) {
  70.  
  71.       Statement stmt;
  72.       PreparedStatement pstmt;
  73.       ResultSet rs;
  74.  
  75.       try {
  76.          // creo la tabella
  77.          stmt = c.createStatement();
  78.          stmt.executeUpdate("DROP TABLE IF EXISTS studenti");
  79.          stmt.executeUpdate("CREATE TABLE studenti "
  80.                  + "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
  81.                  + "nome VARCHAR(30) NOT NULL, "
  82.                  + "cognome VARCHAR(30) NOT NULL, "
  83.                  + "matricola VARCHAR(30) NOT NULL)");
  84.          // inserisco due record
  85.          pstmt = c.prepareStatement("INSERT INTO studenti (nome, cognome, matricola) values (?,?)");
  86.          pstmt.setString(1, "Luca");
  87.          pstmt.setString(2, "Bonaldo");
  88.          pstmt.setString(3, "098345");
  89.          pstmt.execute();
  90.          pstmt.setString(1, "Mario");
  91.          pstmt.setString(2, "Rossi");
  92.          pstmt.setString(3, "098FA1");
  93.          pstmt.execute();
  94.          pstmt.setString(1, "Marco");
  95.          pstmt.setString(2, "Bianchi");
  96.          pstmt.setString(3, "112BB7");
  97.          pstmt.execute();
  98.       } catch (SQLException ex) {
  99.          Logger.getLogger(ReportTest.class.getName()).log(Level.SEVERE, null, ex);
  100.       }
  101.  
  102.    }
  103.  
  104.    public static void main(String[] args) {
  105.       ReportTest rt = new ReportTest();
  106.  
  107.    }
  108. }


file .jrxml
Codice sorgente - presumibilmente Java

  1. <?xml version="1.0"?>
  2. <!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd" [
  3.   <!ENTITY reportFonts SYSTEM "./ReportFonts.ent">
  4. ]>
  5.  
  6. <jasperReport
  7.    name="GeneralTest"
  8.    pageWidth="595"
  9.    pageHeight="842"
  10.    columnWidth="515"
  11.    columnSpacing="0"
  12.    leftMargin="40"
  13.    rightMargin="40"
  14.    topMargin="50"
  15.    bottomMargin="50"
  16.    whenNoDataType="AllSectionsNoDetail"
  17.    isTitleNewPage="false"
  18.    isSummaryNewPage="false">
  19.  
  20.         &reportFonts;
  21.  
  22.    <parameter name="ReportTitle" class="java.lang.String"/>
  23.    <parameter name="FilterClause" class="java.lang.String"/>
  24.    <parameter name="OrderClause" class="java.lang.String"/>
  25.    <queryString><![CDATA[SELECT * FROM studenti]]>
  26.    </queryString>
  27.    <field name="id" class="java.lang.Integer"/>
  28.    <field name="nome" class="java.lang.String"/>
  29.    <field name="cognome" class="java.lang.String"/>
  30.    <field name="matricola" class="java.lang.String"/>
  31.  
  32.    <variable name="Count" class="java.lang.Integer" resetType="Group" resetGroup="CountGroup" calculation="System">
  33.       <initialValueExpression>($V{Count} != null)?(new Integer($V{Count}.intValue() + 1)):(new Integer(1))</initialValueExpression>
  34.    </variable>
  35.    <group name="CountGroup" minHeightToStartNewPage="60">
  36.       <groupExpression>
  37.          $F{count}
  38.       </groupExpression>
  39.       <groupHeader>
  40.          <band height="20">
  41.             <rectangle>
  42.                <reportElement x="0" y="4" width="515" height="15" forecolor="#C0C0C0" backcolor="#C0C0C0"/>
  43.             </rectangle>
  44.             <textField>
  45.                <reportElement x="0" y="4" width="515" height="15" backcolor="#C0C0C0" mode="Opaque"/>
  46.                <textElement textAlignment="Left">
  47.                   <font reportFont="Arial_Bold"/>
  48.                </textElement>
  49.                <textFieldExpression class="java.lang.String">
  50.                   "  " + String.valueOf($V{CountNumber})
  51.                </textFieldExpression>
  52.             </textField>
  53.             <line>
  54.                <reportElement x="0" y="19" width="515" height="0"/>
  55.             </line>
  56.          </band>
  57.       </groupHeader>
  58.       <groupFooter>
  59.          <band height="20">
  60.             <line>
  61.                <reportElement x="0" y="-1" width="515" height="0"/>
  62.             </line>
  63.             <staticText>
  64.                <reportElement x="400" y="0" width="60" height="15"/>
  65.                <textElement textAlignment="Right" lineSpacing="Single">
  66.                   <font reportFont="Arial_Bold"/>
  67.                </textElement>
  68.                <text>Count : </text>
  69.             </staticText>
  70.             <textField>
  71.                <reportElement x="460" y="0" width="30" height="15"/>
  72.                <textElement textAlignment="Right" lineSpacing="Single">
  73.                   <font reportFont="Arial_Bold"/>
  74.                </textElement>
  75.                <textFieldExpression class="java.lang.Integer">
  76.                   $V{CountGroup_COUNT}
  77.                </textFieldExpression>
  78.             </textField>
  79.          </band>
  80.       </groupFooter>
  81.    </group>
  82.    <title>
  83.       <band height="50">
  84.          <line>
  85.             <reportElement x="0" y="0" width="515" height="0" forecolor="black"/>
  86.          </line>
  87.          <textField isBlankWhenNull="true">
  88.             <reportElement x="0" y="10" width="515" height="30"/>
  89.             <textElement textAlignment="Center" lineSpacing="Single">
  90.                <font reportFont="Arial_Normal" size="22"/>
  91.             </textElement>
  92.             <textFieldExpression class="java.lang.String">$P{ReportTitle}</textFieldExpression>
  93.          </textField>
  94.       </band>
  95.    </title>
  96.    <pageHeader>
  97.       <band height="20">
  98.          <rectangle>
  99.             <reportElement x="0" y="5" width="515" height="15" forecolor="#333333" backcolor="#333333"/>
  100.          </rectangle>
  101.          <staticText>
  102.             <reportElement x="0" y="5" width="55" height="15" forecolor="white" backcolor="#333333" mode="Opaque"/>
  103.             <textElement textAlignment="Center">
  104.                <font reportFont="Arial_Bold"/>
  105.             </textElement>
  106.             <text>Nome</text>
  107.          </staticText>
  108.          <staticText>
  109.             <reportElement x="55" y="5" width="205" height="15" forecolor="white" backcolor="#333333" mode="Opaque"/>
  110.             <textElement>
  111.                <font reportFont="Arial_Bold"/>
  112.             </textElement>
  113.             <text>Cognome</text>
  114.          </staticText>
  115.          <staticText>
  116.             <reportElement x="260" y="5" width="255" height="15" forecolor="white" backcolor="#333333" mode="Opaque"/>
  117.             <textElement>
  118.                <font reportFont="Arial_Bold"/>
  119.             </textElement>
  120.             <text>Matricola</text>
  121.          </staticText>
  122.       </band>
  123.    </pageHeader>
  124.    <detail>
  125.       <band height="20">
  126.          <textField isStretchWithOverflow="true">
  127.             <reportElement x="55" y="4" width="200" height="15" positionType="Float"/>
  128.             <textFieldExpression class="java.lang.String">
  129.                $F{nome}
  130.             </textFieldExpression>
  131.          </textField>
  132.          <textField isStretchWithOverflow="true">
  133.             <reportElement x="260" y="4" width="255" height="15" positionType="Float"/>
  134.             <textFieldExpression class="java.lang.String">
  135.                $F{cognome}
  136.             </textFieldExpression>
  137.          </textField>
  138.          <textField isStretchWithOverflow="true">
  139.             <reportElement x="260" y="4" width="255" height="15" positionType="Float"/>
  140.             <textFieldExpression class="java.lang.String">
  141.                $F{matricola}
  142.             </textFieldExpression>
  143.          </textField>
  144.          <line>
  145.             <reportElement x="0" y="19" width="515" height="0" forecolor="#808080" positionType="Float"/>
  146.          </line>
  147.       </band>
  148.    </detail>
  149.    <pageFooter>
  150.       <band height="40">
  151.          <line>
  152.             <reportElement x="0" y="10" width="515" height="0"/>
  153.          </line>
  154.          <textField>
  155.             <reportElement x="200" y="20" width="80" height="15"/>
  156.             <textElement textAlignment="Right"/>
  157.             <textFieldExpression class="java.lang.String">
  158.                "Page " + String.valueOf($V{PAGE_NUMBER}) + " of"
  159.             </textFieldExpression>
  160.          </textField>
  161.          <textField evaluationTime="Report">
  162.             <reportElement x="280" y="20" width="75" height="15"/>
  163.             <textElement textAlignment="Left"/>
  164.             <textFieldExpression class="java.lang.String">
  165.                " " + String.valueOf($V{PAGE_NUMBER})
  166.             </textFieldExpression>
  167.          </textField>
  168.       </band>
  169.    </pageFooter>
  170.    <summary>
  171.       <band height="60">
  172.          <textField isStretchWithOverflow="true">
  173.             <reportElement x="175" y="20" width="165" height="15"/>
  174.             <textElement textAlignment="Center">
  175.                <font reportFont="Arial_Italic"/>
  176.             </textElement>
  177.             <textFieldExpression class="java.lang.String">
  178.                "Ci sono " +
  179.                String.valueOf($V{REPORT_COUNT}) +
  180.                " studenti in questo report."
  181.             </textFieldExpression>
  182.          </textField>
  183.       </band>
  184.    </summary>
  185. </jasperReport>


come vedete uso un db sqlite  ma questo non è il problema..

Io uso Netbeans queste sono le librerie che ho aggiunto al progetto:

sqlite-jdbc.jar
jasperreport.jar
commons-degester.jar
commons-logging.jar

ovviamente aggiornate alle ultime versioni..

quando avvio il programma segue questa eccezione:

run:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
    at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:110)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:415)
    at provajasper.ReportTest.<init>(ReportTest.java:36)
    at provajasper.ReportTest.main(ReportTest.java:110)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.digester.Digester
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 4 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

ma come vedete io ho importato le librerie...

vi allego l'intero progetto, sto "smanettando" da ore e non ne vengo a capo, per favore se riuscite datemi un consiglio su come risolvere la questione..

Ultima modifica effettuata da Bonny il 13/09/2012 alle 17:35
PM Quote
Avatar
fraioveio (Member)
Rookie


Messaggi: 34
Iscritto: 27/03/2011

Segnala al moderatore
Postato alle 14:40
Lunedì, 17/09/2012
Avevo anch'io lo stesso problema...
JasperReport ha una cartella lib, devi includere anche le librerie in quella cartella :k:

PM Quote
Avatar
Bonny (Member)
Expert


Messaggi: 437
Iscritto: 24/04/2009

Segnala al moderatore
Postato alle 20:22
Lunedì, 17/09/2012
Grazie mille!!!!

PM Quote