简体   繁体   中英

why a valid date insertion throws java.sql.SQLDataException: ORA-01843: not a valid month oracle java

i am trying to insert date value into the oracle database but its throw java.sql.SQLDataException: ORA-01843: not a valid month the weirdest thing that happened, when i open oracle dashboard and i execute the same query into sql commands its work perfectly but when try to execute the query from java it launch the exception

this is my code

try {
        
            Connection con=Connexion.getConnection();
            Statement stmt=con.createStatement();
            Format formatter = new SimpleDateFormat("MM/dd/YYYY");
            String ddebut=formatter.format(dateDebut);
            String dfin=formatter.format(dateFin);

            System.out.println(ddebut);
            System.out.println(dfin);
            String query ="INSERT INTO Intervention (titre,lieu,etat,description,datedebut,datefin,ide) VALUES ('"+titre+"','"+lieu+"','"+etat+"','"
                    +description+"','"+ddebut+"','"+dfin+"',"+cin+")";
            System.out.println(query);

            test=stmt.executeUpdate(query);
            System.out.println(formatter.format(dateDebut));
            ResultSet rs=stmt.executeQuery("select intervention_seq.currval from DUAL");
            rs.next();
            id=rs.getInt(1);
        
        
            con.close();  
        
    

        }catch(Exception e){ System.out.println(e);}
        return test==1;
    }

im sure that the problem come from the query and all values of attribute are correct

is there a way to solve this problem? 在此处输入图像描述

It's almost surely an NLS issue. Try supplying your dates as '20/05/2021' and '19/05/2021'. Each client (for example, Oracle dashboard and your Java program are two different clients) can have different NLS settings. These govern, among other things, the default date and time formatting.

Oracle accepts date formatted in DD-MON-RR (sometimes DD-MON-YYYY) by default. However, you've specified MM/dd/YYYY.

Solution 1
You can simply fix this by changing the date format from this,
new SimpleDateFormat("MM/dd/YYYY")
to this,
new SimpleDateFormat("dd-MMM-yy") (If the default format is DD-MON-RR)
or this,
new SimpleDateFormat("dd-MMM-yyyy") (If the default format is DD-MON-YYYY)

Note: You can check the default format simply by just select sysdate from dual; and see what does it returns. If it shows something like 01-MAY-21 , then the default date format for your Oracle should be DD-MON-RR.

Solution 2
Keep your SimpleDateFormat, but changes the date format at your SQL statement by using to_date('date here', "MM/DD/YYYY")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM