Reputation: 9201
I am trying to learn JasperReports
on some online tutorial sites but I cant seem to make this simple snippet work.
I wanted to select a date field from my DB and pass it into my jrxml file.
<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="ToolDemo"
columnCount="2"
columnWidth="240" pageWidth="612" pageHeight="792">
<import value="java.util.Date"/>
<queryString>
<![CDATA[select a.id, a.date_acquired from tools a where a.country = 'Oceania']]>
</queryString>
<field name="id" class="java.lang.String"/>
<field name="date_acquired" class="java.util.Date"/>
But I cant get pass the filling process and I am having an error: Unable to get value for field date_acquired of class "java.util.Date"
. I tried making use of import tag also.
Any idea please?
Upvotes: 0
Views: 8503
Reputation: 1176
I've copied your report template, create sample MySql database with one table
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | varchar(20) | YES | | NULL | |
| a_date | date | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
Then I inserted one row:
insert into tools(id, a_date) values('1', '2010-05-01');
Test query:
mysql> select * from tools;
+------+------------+
| id | a_date |
+------+------------+
| 1 | 2010-05-01 |
+------+------------+
1 row in set (0.00 sec)
After that I used your template and tested it under iReport tool. Just open you report using iReport.
Your report template with little modifications:
<?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="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<import value="java.util.Date"/>
<queryString>
<![CDATA[SELECT a.id, a.a_date FROM tools a]]>
</queryString>
<field name="id" class="java.lang.String"/>
<field name="a_date" class="java.sql.Date"/>
<title>
<band height="79" splitType="Stretch"/>
</title>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="100" y="24" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="24" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.util.Date"><![CDATA[$F{a_date}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Using mysql database connection and report template i tested report generation under iReport and it works fine.
Are you sure that field "date_acquired" is type of DATE (or another format for dates in your database) in database? I recommend you to use iReport fore report template generation. It's very useful and prevent you from writing a lot of "boring" code. Also, it could helps you to create report fields from sql query and test it using real database.
Upvotes: 1