Jump to content
We've recently updated our Privacy Statement, available here ×

Handling null values in a Date column of ResultSet


oamador

Recommended Posts

This is a 2 part story.

 

First, I'm trying to format a date field in the report. Used the TextField properties to set the pattern to dd-MMM-yy. The field would not use the pattern, so I figured, to change the class of the TextField to Date. Running the report complained that it could not convert from String to Date, so I change the class of the field from String to Date.

 

Ok, now the report format the column using the pattern provided.

 

Q1: Was that the right trail to get iReport to use my pattern for formatting the Date?

 

Second, now that the report is working with the pattern provided, I receiving an error when the date column is null. I have selected to option to print a blank when null in the TextField, but it still issues an exception when running the report.

 

Error filling print... Unable to get value for field 'DATDate' of class 'java.util.Date'
net.sf.jasperreports.engine.JRException: Unable to get value for field 'DATDate' of class 'java.util.Date'

 

I noticed that if I change the field class back to a String, it will handle the null value properly and print the blank. But then, I'm back to square one, the pattern is not used for formating the Date.

 

Q2: How to handle null values in a Date class column of a ResultSet?

 

I'm using iReport 2.0, JDK 1.6, MS-SQL JDBC driver 1.1.

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

oamador,

 

If you are using SQL as your query language, there is a built in function provided to specifically format a String into a Date. An example of this function could look like this:

 

select to_char("StringToFormat", 'mm-dd-yyyy') as NameOfField,

from Table...

 

This can be done while executing your query, so now you do not have to use the pattern option. The 'Blank when null' option should still be functional as long as there are no other values being displayed inside of your textfield expression. Hope it helps...

Link to comment
Share on other sites

Thanks for the reply.

 

Looking further I found that the column in the view I'm querying, is actually a string and not a date or datetime field, as I assumed. :blush:

 

Since modifying the database is not an option in my case, I opted for writting a function to parse that string into a date and the use a SimpleDateFormat class to format the date. The function is part of a Scriptlet attached to the report.

 

Once again thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...