0

I have a birthdate as string

String memBirthDate = "19990715";

and I tried to insert this into oracle by using query below.

<entry key="insertMember">
    INSERT INTO MEMBER
          (
       MEM_BIRTHDATE
           )
      VALUES (
            (TO_DATE)?
             )
           </entry>

But it doesn't work. I think there's some problem in my oracle query but I'm not sure.

What should I do?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gm4
  • 1

1 Answers1

2

Avoid legacy classes

Never use either Date class. Both are part of the terrible date-time legacy classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

LocalDate

For a date-only value, use LocalDate class.

Your input string complies with the “basic” variation of the ISO 8601 standard format, YYYYMMDD. So use the predefined formatter, DateTimeFormatter.BASIC_ISO_DATE.

String input = "19990715" ;
LocalDate birthDate = LocalDate.parse( input , DateTimeFormatter.BASIC_ISO_DATE ) ;

Write your SQL like the following.

Tip: Use trailing underscore in all your database naming to avoid collision with reserved keywords. The SQL standard promises explicitly to never use a trailing underscore. See this Answer by me.

Another tip: Use text blocks for your embedded SQL code.

String sql  =
        """
        INSERT INTO member_ ( id_ , birth_date_ ) 
        VALUES ( ? , ? ) 
        ;
        """;

Exchange the LocalDate object for writing to database.

myPreparedStatement.setObject( 2 , birthDate ) ;

When retrieving the data value from the database.

LocalDate birthDate = myResultSet.getObject( … , LocalDate.class ) ;

All this has been covered many times on Stack Overflow. Search to learn more. You will find complete source code for example apps that create a table, insert rows, and retrieve rows.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • *"All this has been covered many times on Stack Overflow"* and yet there's no duplicate? Either close as dupe, or if there is none then remove this statement. It makes indeed no sense to repeat the same thing over and over. – BalusC Feb 27 '23 at 12:43