---
name: data-sources
description: Connect JasperReports reports to JDBC, JavaBean, Map, CSV, XML, or JSON data; map fields, bind query parameters safely, add the required data modules, and manage connection and data-source lifecycle. Use for report input, query, field-mapping, or empty/consumed data-source problems, not for layout or styling of an already fixed data contract.
---
# Connect report data
## Establish the data contract
Identify the exact engine/Java versions, the data-source type, how the application fills the report, and who owns the connection. Record each field's name, Java type, nullability and source mapping, plus each parameter's type and whether it reaches a query. Use synthetic data; never request production credentials or connect to a production database to test a report.
## Workflow
1. Add the modules the source needs at the engine's exact version. In the tested 7.1.1 runtime, JDBC, JavaBean, Map, CSV and XML data sources are in core, but JSON and JSONQL (`net.sf.jasperreports.json.data`) need `jasperreports-json`, and `JRXmlDataSource` fails with `JaxenXPathExecuterFactory not found` unless `jasperreports-jaxen` is present. Verify module boundaries for other releases.
2. Map fields deliberately. JDBC fields match column labels; Map fields match keys; JavaBean fields use getter properties (or field descriptions when configured); CSV fields match header or configured column names; JSON and XML fields use the field description as a path relative to the selected records. Declare actual types such as `java.math.BigDecimal` and check conversions.
3. Treat nulls by source. A SQL `NULL`, absent Map key or JSON `null` arrives as null, but a blank CSV text column arrives as an empty String. Make no-data behavior explicit with `whenNoDataType` and null-safe expressions and totals.
4. Bind query values with `$P{...}`, which becomes a prepared-statement parameter. Use `$X{...}` for supported generated clauses such as collection `IN`. `$P!{...}` substitutes raw SQL text before the query runs: never pass user input to it. If an identifier such as a sort column must vary, map an application-side enum or allowlist to fixed SQL text and reject everything else before filling.
5. Give each fill its own cursor. A `JRDataSource` is consumed by the fill that reads it; reusing the same instance silently produces an empty report. Create a new data source per fill, reuse the filled `JasperPrint` for several exports of the same output, or use `JRRewindableDataSource.moveFirst()` only where its semantics fit. Subreports and dataset runs need their own data source expressions.
6. The caller owns a passed JDBC `Connection`: obtain it from the application's pool with least privilege, close it with try-with-resources, and do not share it across concurrent fills unless the driver and pool allow it. Keep query timeouts and fetch sizes in the application's normal configuration.
## Validate and deliver
Fill with the consuming project's engine and synthetic input covering ordinary, null and empty rows. Assert row counts and typed totals, fill twice to prove fresh cursors, and pass an injection-shaped string (for example `x' OR '1'='1`) to each bound parameter to show it is treated as data. Use an in-memory or disposable test database. Report the exact modules, commands and results; label any data-source behavior you could not execute as unverified.
## Sources
- [Data source samples](https://jasperreports.sourceforge.net/sample.reference/README.html)
- [API](https://jasperreports.sourceforge.net/api/index.html)
- [Release changes](https://github.com/Jaspersoft/jasperreports/blob/master/changes.txt)
— ‹guest · 2026-11-29› —
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.