Search code examples
javasql-servernetbeanssqlexception

Java.sql.sqlexception: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value


When I am updating my Date Field the update does not works from NetBeans and Below exception is raised

Java.sql.sqlexception: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value

However I run the same query the query works fine in SQL Query Analyzer (SQL Server 2000)

I am formatting the Date Value to be updated as follows in NetBeans.

        JXDatePicker jDatePicker = (JXDatePicker) comp;
        Date date = jDatePicker.getDate();
        if (date != null) {
        try {
            String expectedPattern = "yyyy-MM-dd HH:mm:ss.SSS";
            String currentFormat = "dd-MM-yyyy HH:mm:ss.SSS";

            SimpleDateFormat dateFormatReq = new SimpleDateFormat(expectedPattern);

            SimpleDateFormat dateFormatCurr = new SimpleDateFormat(currentFormat);          

            // To Convert Date To Required Format For DB

            //First Prepare A String In Current Format dd-MM-yyyy
            // Then Convert To The Date In The Current Format dd-MM-yyyy
            // Then Convert Into  String In The Desired Format yyyy-MM-dd
            String strDate =     dateFormatReq.format(dateFormatCurr.parse(dateFormatCurr.format(date)));

        } catch (ParseException ex) {
        Logger.getLogger(ChangeJControlProperties.class.getName()).log(Level.SEVERE, null, ex);
        }

        } 

String query =update myTable
               set date=strDate  where id=myDd

For Date 2013-07-05 it updates successfully.

For Date 2013-06-28 it displays the above exception


Solution

  • The problem has been solved as the Default Language Selected was Thai, when i change it to English the exception does not come and the problem disappears.