Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Il documento non ha pagine. Rapporto Jasper

In primo luogo, la gestione delle risorse...

Dovresti aprire solo una singola connessione al database, se possibile. Assicurati di chiuderlo prima che l'applicazione venga chiusa. Il processo di connessione può essere costoso, quindi vuoi farlo davvero solo quando devi assolutamente...

Chiudi le tue risorse una volta che hai finito con esse. Ciò si ottiene utilizzando un try-finally blocco...

private Connection con;

protected void close() throws SQLException {
    if (con != null) {
        con.close();
    }
}

protected Connection getConnection() throws ClassNotFoundException, SQLException {
    if (con == null) {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String url = "jdbc:odbc:*****";
        String user = "******";
        String pass = "******";
        Connection con = DriverManager.getConnection(url, user, pass);
    }
    return con;
}

private void search() throws Exception {

    Statement state = null;
    ResultSet rs = null;

    try {

        state = getConnection().createStatement();

        rs = state.executeQuery("SELECT "
                + "pIDNo AS 'Patient ID',"
                + "pLName AS 'Last Name',"
                + "pFName AS 'First Name',"
                + "pMI AS 'M.I.',"
                + "pSex AS 'Sex',"
                + "pStatus AS 'Status',"
                + "pTelNo AS 'Contact No.',"
                + "pDocID AS 'Doctor ID',"
                + "pAddr AS 'St. No.',"
                + "pStreet AS 'St. Name',"
                + "pBarangay AS 'Barangay',"
                + "pCity AS 'City',"
                + " pProvince AS 'Province',"
                + " pLNameKIN AS 'Last Name',"
                + "pFNameKIN AS 'First Name',"
                + "pMIKIN AS 'M.I.',"
                + "pRelationKIN AS 'Relation',"
                + "pTotalDue AS 'Total Due'"
                + " FROM dbo.Patients");
        ResultSetMetaData rsmetadata = rs.getMetaData();
        int columns = rsmetadata.getColumnCount();

        DefaultTableModel dtm = new DefaultTableModel();
        Vector column_name = new Vector();
        Vector data_rows = new Vector();

        for (int i = 1; i < columns; i++) {
            column_name.addElement(rsmetadata.getColumnName(i));
        }
        dtm.setColumnIdentifiers(column_name);

        while (rs.next()) {
            data_rows = new Vector();
            for (int j = 1; j < columns; j++) {
                data_rows.addElement(rs.getString(j));
            }
            dtm.addRow(data_rows);
        }
        tblPatient.setModel(dtm);

    } finally {
        try {
            rs.close();
        } catch (Exception e) {
        }
        try {
            state.close();
        } catch (Exception e) {
        }
    }
}

Ora il problema in questione...

Sembra che tu abbia creato due riferimenti a con . Uno come campo di classe e uno come variabile di metodo (in search ).

Stai quindi passando con a Jasper Reports, che sospetto sia null . Invece dovresti usare getConnection() come indicato sopra.

public void reportviewer() {
    try{
        String report = "C:\\Users\\cleanfuel\\Documents\\NetBeansProjects\\StringManipulation\\src\\stringmanipulation\\report1.jrxml";
        JasperReport jasp_report = JasperCompileManager.compileReport(report);
        JasperPrint jasp_print = JasperFillManager.fillReport(jasp_report, null, getConnection());
        JasperViewer.viewReport(jasp_print);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Aggiornato con lavoratore in background...

La compilazione e la compilazione di un report può richiedere del tempo. Dovresti scaricare questo lavoro su un thread in background in modo che non interferisca con la tua interfaccia utente (o faccia sembrare che la tua applicazione sia bloccata).

La soluzione più semplice sarebbe usare un SwingWorker . Ha funzionalità per risincronizzare i thread con l'interfaccia utente

public void reportviewer() {
    // Disable any UI components you don't want the user using while
    // the report generates...
    new ReportWorker().execute();
}

public class ReportWorker extends SwingWorker<JasperPrint, Void> {

    @Override
    protected JasperPrint doInBackground() throws Exception {
        String report = "C:\\Users\\cleanfuel\\Documents\\NetBeansProjects\\StringManipulation\\src\\stringmanipulation\\report1.jrxml";
        JasperReport jasp_report = JasperCompileManager.compileReport(report);
        JasperPrint jasp_print = JasperFillManager.fillReport(jasp_report, null, getConnection());
        return jasp_print;
    }

    @Override
    protected void done() {
        try {
            JasperPrint jasp_print = get();
            JasperViewer.viewReport(jasp_print);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        // Renable any UI components you disabled before the report run
    }
}

Dai un'occhiata a Concurrency in Swing per maggiori dettagli.

Suggerimenti

Se puoi precompilare il rapporto e caricarlo (anziché caricare l'XML), il processo del rapporto sarà più rapido.