我试图用JasperReports和iReport完成我的第一步。最近我解决了this post中数据库连接的问题。之后,我创建了另一个成功连接到MySQL数据库,以加强知识;我提出了一个问题,但现在我得到了另一个错误 - 我猜想 - 对于查询本身或类似的东西。
当我尝试创建报表查询时,会检索字段(表格列)并正确识别其类型,但数据(行)会在消息中显示错误
Error: net.sf.jasperreports.engine.JRException: Error excecuting SQL statement for: data
错误显示在下面的屏幕截图中:
然后,当我尝试使用查询字段预览报表时,出现以下错误:
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid :
1. Field not found : idaddress
2. Field not found : city
at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:271)
at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:153)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:354)
at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:270)
at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:563)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:528)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
在jrxml
文件中,我在this other post中看到的字段已正确定义,如下所示:
<subDataset name="MyDataset" uuid="b9d7b569-d19e-451e-8a15-c558918e1698">
<queryString language="SQL">
<![CDATA[SELECT
address.`idaddress` AS address_idaddress,
address.`city` AS address_city
FROM
`address` address]]>
</queryString>
<field name="address_idaddress" class="java.lang.Integer"/>
<field name="address_city" class="java.lang.String"/>
</subDataset>
完整的jrxml
文件是:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PrimerReporte" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eb8d4459-c8dc-4c96-bc35-ac988007e32a">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#A52A2A">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#FFBFBF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#FFEFEF"/>
</conditionalStyle>
</style>
<subDataset name="MyDataset" uuid="b9d7b569-d19e-451e-8a15-c558918e1698">
<queryString language="SQL">
<![CDATA[SELECT
address.`idaddress` AS address_idaddress,
address.`city` AS address_city
FROM
`address` address]]>
</queryString>
<field name="address_idaddress" class="java.lang.Integer"/>
<field name="address_city" class="java.lang.String"/>
</subDataset>
<parameter name="idaddress" class="java.lang.Integer">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<parameter name="city" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT
address.`idaddress` AS address_idaddress,
address.`city` AS address_city
FROM
`address` address]]>
</queryString>
<field name="address_idaddress" class="java.lang.Integer"/>
<field name="address_city" class="java.lang.String"/>
<filterExpression><![CDATA[$F{idaddress}]]></filterExpression>
<group name="city">
<groupExpression><![CDATA[$F{city}]]></groupExpression>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="121" y="0" width="313" height="49" uuid="e3c5e2b7-14a4-4e26-a90c-ae4702f4ac65"/>
<textElement>
<font size="36"/>
</textElement>
<textFieldExpression><![CDATA[Mi primer reporte]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="22" y="0" width="510" height="35" uuid="7d375361-1b1d-4a7b-8788-9dd432b86c43"/>
<textElement>
<font size="18"/>
</textElement>
<text><![CDATA[Este es un reporte de prueba para aprender a usar iReport]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement key="table" style="table" x="0" y="0" width="401" height="80" uuid="8c775bc1-4076-44c4-a7af-1fc405283908"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="MyDataset" uuid="12ca7d6a-d625-42ab-bff9-57937d50f096">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(1)]]></dataSourceExpression>
</datasetRun>
<jr:column width="200" uuid="37b69ab0-be68-4dda-8739-08860a5dd8c7">
<jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="200" height="30" uuid="91dcc02e-4f54-446e-9092-d8e8fef6bc78"/>
<textFieldExpression><![CDATA[$F{address_idaddress}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1"/>
</jr:column>
<jr:column width="200" uuid="36dc21c0-a551-4df8-a180-95cf5b9cfc0c">
<jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="200" height="30" uuid="294719c1-d37b-4b53-ab0e-2045397c0d60"/>
<textFieldExpression><![CDATA[$F{address_city}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1"/>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
连接的配置是这样的:
注意:是的,服务器没有密码,它只是一个测试环境......
我在同一篇文章中尝试了其他答案,但它们都不适合我。最后我尝试更改连接URL添加端口和用户,但错误保持不变。
不知道为什么在<subDataset>
元素下面我有以下XML:
<queryString>
<![CDATA[SELECT
*
FROM
`address` address
ORDER BY
city ASC]]>
</queryString>
<field name="idaddress" class="java.lang.Integer">
<fieldDescription><![CDATA[Address table ID]]></fieldDescription>
</field>
<field name="city" class="java.lang.String">
<fieldDescription><![CDATA[The city]]></fieldDescription>
</field>
<group name="city">
<groupExpression><![CDATA[$F{city}]]></groupExpression>
</group>
看起来与<subDataset>
相同,但它就在那里。我不想删除,因为这是由iReport生成的,所以我认为它是正确的。
所以问题是:我在这里缺少什么?我可以尝试解决这个问题?我在哪里可以寻找一些线索?
提前感谢您的回答。
答案 0 :(得分:1)
您对Jasper Report的工作原理,主数据源与subDataset之间的区别有一些疑惑,使用它们时,您需要引用与您正在使用的数据源相关的字段。
澄清一些困惑:
主数据源将执行,jasper报告将迭代 (重复)每条记录的细节带。
您可以传递给组件的subDatset为another datasource。
您的报告的解决方案是仅使用主查询(删除jr:table组件及其子数据集),或者如果您想使用jr:table
,请使用仅返回1条记录的主数据源,然后将子数据源和连接传递给jr:table
组件
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PrimerReporte" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eb8d4459-c8dc-4c96-bc35-ac988007e32a">
<queryString>
<![CDATA[SELECT
address.`idaddress` AS address_idaddress,
address.`city` AS address_city
FROM
`address` address]]>
</queryString>
<field name="address_idaddress" class="java.lang.Integer"/>
<field name="address_city" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="121" y="0" width="313" height="49" uuid="e3c5e2b7-14a4-4e26-a90c-ae4702f4ac65"/>
<textElement>
<font size="36"/>
</textElement>
<textFieldExpression><![CDATA[Mi primer reporte]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="22" y="0" width="510" height="35" uuid="7d375361-1b1d-4a7b-8788-9dd432b86c43"/>
<textElement>
<font size="18"/>
</textElement>
<text><![CDATA[Este es un reporte de prueba para aprender a usar iReport]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="2604ec98-ab97-4192-9a22-c6c4f95b2416"/>
<text><![CDATA[address_idaddress]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20" uuid="afb38cfb-3eeb-4af9-a520-93338b12eed9"/>
<text><![CDATA[address_city]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="aabd4a33-ecb9-4c60-b366-f770a01c32f8"/>
<textFieldExpression><![CDATA[$F{address_idaddress}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="58913924-4ffe-4e78-a783-b2364f66c27a"/>
<textFieldExpression><![CDATA[$F{address_city}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
注意:不再有表组件和子数据源,主数据源的字段直接在
textField
中引用。细节带将重复与查询中的记录一样多的时间。
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PrimerReporte" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eb8d4459-c8dc-4c96-bc35-ac988007e32a">
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#A52A2A">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#FFBFBF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#FFEFEF"/>
</conditionalStyle>
</style>
<subDataset name="MyDataset" uuid="b9d7b569-d19e-451e-8a15-c558918e1698">
<queryString language="SQL">
<![CDATA[SELECT
address.`idaddress` AS address_idaddress,
address.`city` AS address_city
FROM
`address` address]]>
</queryString>
<field name="address_idaddress" class="java.lang.Integer"/>
<field name="address_city" class="java.lang.String"/>
</subDataset>
<queryString>
<![CDATA[SELECT 1]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="121" y="0" width="313" height="49" uuid="e3c5e2b7-14a4-4e26-a90c-ae4702f4ac65"/>
<textElement>
<font size="36"/>
</textElement>
<textFieldExpression><![CDATA[Mi primer reporte]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="22" y="0" width="510" height="35" uuid="7d375361-1b1d-4a7b-8788-9dd432b86c43"/>
<textElement>
<font size="18"/>
</textElement>
<text><![CDATA[Este es un reporte de prueba para aprender a usar iReport]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement key="table" style="table" x="0" y="0" width="401" height="80" uuid="8c775bc1-4076-44c4-a7af-1fc405283908"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="MyDataset" uuid="ec302d39-8b71-4c05-86c0-74bb5a2b71d4">
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>
<jr:column width="200" uuid="37b69ab0-be68-4dda-8739-08860a5dd8c7">
<jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="200" height="30" uuid="91dcc02e-4f54-446e-9092-d8e8fef6bc78"/>
<textFieldExpression><![CDATA[$F{address_idaddress}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1"/>
</jr:column>
<jr:column width="200" uuid="36dc21c0-a551-4df8-a180-95cf5b9cfc0c">
<jr:tableHeader style="table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="200" height="30" uuid="294719c1-d37b-4b53-ab0e-2045397c0d60"/>
<textFieldExpression><![CDATA[$F{address_city}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1"/>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
注意:主查询如何只有
SELECT 1
,mysql语句将返回1(在1条记录中)以及如何传递$P{REPORT_CONNECTION}
以便subDataset可以执行查询。