Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value '0000-00-00' can not be represented as java.sql.Date

I'm working on some project which needs to extract data from DB and I use Spring MVC to build model from DB for selecting data.

Here is the problem with my JSP page:

<form action="result" method="get" >
<table>
<thead>
<tr>
<th>Date to select:</th>
<th>Name to select:</th>
<th>Type to select:</th>
</tr>
</thead>

<tbody>
<tr>
<td><form:select  path="listOfDates">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfDates}"></form:options>
</form:select>
</td>  
<td><form:select  path="listOfInstitutionsNames">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfInstitutionsNames}"></form:options>
</form:select>
</td>
<td>
<form:select  path="listOfInstitutionsTypes">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfInstitutionsTypes}"></form:options>
</form:select>
</td>
</tr>
</tbody>

<tfoot>
<tr>
<td><input type="submit" value="Извлечь"/></td>
</tr>
</tfoot>

</table>
</form>

As you can see I try to use <form:select> from Spring tag library.
Question:
but when it's preparing my model with this controller:

@Controller
public class HomeController{ 

    @Autowired
    private ControllerSupportClass controllerSupportClass; 


        @RequestMapping(value="/search", method=RequestMethod.GET)
        public String search(Model model) {
            List<Date> listOfDates = controllerSupportClass.findAllDatesForm();
            List<String> listOfInstitutionsNames = controllerSupportClass.findAllInstitutionsForm();
            List<String> listOfInstitutionsTypes = controllerSupportClass.findAllTypesForm();
            model.addAttribute("listOfInstitutionsTypes", listOfInstitutionsTypes);
            model.addAttribute("listOfInstitutionsNames", listOfInstitutionsNames);
            model.addAttribute("listOfDates", listOfDates);
            return "search";    
        }


        @RequestMapping(value ="/result", method=RequestMethod.GET)
        public String SecondActionPage(@RequestParam String particularDate, 
                                       @RequestParam String nameOfInstitution, 
                                       @RequestParam String typeName,
                                       Model model) throws Exception {


                if(particularDate !="" && nameOfInstitution.trim() !="" && typeName.trim()=="") {                   
                    controllerSupportClass.findWithDateAndName(nameOfInstitution, particularDate, model);                   
                } else if(particularDate.trim() !="" && nameOfInstitution.trim() =="" && typeName.trim() !="") {                    
                    controllerSupportClass.findWithAddedDateAndType(typeName, particularDate, model);                   
                } else if(particularDate.trim() !="" && nameOfInstitution.trim() =="" && typeName.trim() ==""){         
                    controllerSupportClass.findWithAddedDate(particularDate, model);    
                } else if(particularDate.trim() !="" && nameOfInstitution.trim() !="" && typeName.trim() !="") {
                    throw new Exception("Search by choose all parameters is not exceptable");   
                } else {    
                    throw new Exception("You didn't put any search parameters");    
                }           
            return "search";
        }

}

It gives me an error like this:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/controller/] in DispatcherServlet with name 'appServlet' Hibernate: select distinct creationda0_.DATE_ID as DATE1_0_, creationda0_.CHILD_ADMISSION_DATE as CHILD2_0_, creationda0_.CHILD_GO_SCHOOL_DATE as CHILD3_0_, creationda0_.PARTICULAR_DATE as PARTICULAR4_0_, creationda0_.VERSION as VERSION0_ from CREATION_DATE creationda0_ WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: S1009 ERROR: org.hibernate.util.JDBCExceptionReporter - Value '0000-00-00' can not be represented as java.sql.Date Jun 19, 2013 3:26:53 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [appServlet] in context with path [/controller] threw exception [Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date

If you need my Entity class it's here I'm using Date from java.util, but I go with @Temporal annotation to convert it from SQL to unit Date as I understand:

@Entity
@Table(name="CREATION_DATE")
public class CreationDate implements Serializable {


    private int dateId;

        @Id
        @GeneratedValue(strategy=IDENTITY)
        @Column(name="DATE_ID")
        public int getDateId() {
            return dateId;
        }

        public void setDateId(int dateId) {
            this.dateId = dateId;
        }


    private int version;

        @Version
        @Column(name="VERSION")
        public int getVersion() {
            return version;
        }

        public void setVersion(int version) {
            this.version = version;
        }


    private Date particularDate;

        @Temporal(TemporalType.DATE)
        @DateTimeFormat(pattern="yyyy-MM-dd")
        @Column(name="PARTICULAR_DATE")
        public Date getParticularDate() {
            return particularDate;
        }

        public void setParticularDate(Date particularDate) {
            this.particularDate = particularDate;
        }


    private Date childGoSchoolDate;

        @Temporal(TemporalType.DATE)
        @DateTimeFormat(pattern="yyyy-MM-dd")
        @Column(name="CHILD_GO_SCHOOL_DATE")
        public Date getChildGoSchoolDate() {
            return childGoSchoolDate;
        }

        public void setChildGoSchoolDate(Date childGoSchoolDate) {
            this.childGoSchoolDate = childGoSchoolDate;
        }


    private Date childAdmissionDate;

        @Temporal(TemporalType.DATE)
        @DateTimeFormat(pattern="yyyy-MM-dd")
        @Column(name="CHILD_ADMISSION_DATE")
        public Date getChildAdmissionDate() {
            return childAdmissionDate;
        }

        public void setChildAdmissionDate(Date childAdmissionDate) {
            this.childAdmissionDate = childAdmissionDate;
        }


}

Assuming that the problem with data type conversion is because MVC uses String, but the actual type is Date.

like image 326
java_user Avatar asked Jun 19 '13 15:06

java_user


People also ask

What is the format of Java sql date?

Formats a date in the date escape format yyyy-mm-dd. Converts a string in JDBC date escape format to a Date value.

Does Java sql date have time?

Its main purpose is to represent SQL DATE, which keeps years, months and days. No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java.

What is the format of Java sql timestamp?

Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.


2 Answers

In MySql '0000-00-00' is considered a valid date, but it can't be repesented as java.sql.Date.

You could use a query that returns NULL in case date is '0000-00-00', or the actual value otherwise:

SELECT
  CASE WHEN `date`!='0000-00-00' THEN `date` END new_date
FROM
  yourtable

or you can add to your datasource connection string this:

zeroDateTimeBehavior=convertToNull

and dates as '0000-00-00' will be automatically converted to NULL.

like image 111
fthiella Avatar answered Sep 28 '22 07:09

fthiella


Append the following statement with the JDBC-mysql protocol:

"?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8"

for Example:

jdbc:mysql://localhost/infra?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8
like image 43
Yogesh H Bhosale Avatar answered Sep 28 '22 05:09

Yogesh H Bhosale