Java createNativeQuery Insert datetime

Asked

Viewed 158 times

0

I’m trying to insert into a bank whose dates are datetime2(7).

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

Query u = em.createNativeQuery(insert);
u.setParameter(1, sdf );

You’re making the following mistake:

Implicit Conversion from data type varbinary to date is not allowed. Use the CONVERT Function to run this query.

1 answer

0

SimpleDateFormat is not a date. This class is used to format dates (transform Date for String) and make Parsing (transform String in Date) - see the documentation.

If the field in the database is a date, pass the Date - java.util.Date, or java.sql.Date (or java.sql.Timestamp, or whatever you’re using) to setParameter.


If you have a String and want to save the date, in this case you use the SimpleDateFormat to convert the String for Date:

Date date = sdf.parse(stringQueContemAData);

And then pass the Date for the method setParameter:

u.setParameter(1, date);

But as you did not specify which input and output (if it is String, Date, etc), you can’t help more than that.


Dates have no format, only values (see this article that explains this subtle difference). Just because you are seeing the date in a specific format does not necessarily mean that it is in that format.

Internally, each language, API, or database records data types in an internal format, but it doesn’t matter who uses them. When the date is displayed (printed, recorded in a file, shown on the screen, etc.), then a display format is chosen. But that doesn’t mean that the date is recorded in that format.

That being said, if the field in the bank is of the datetime type, just pass the Date for setParameter.

  • how do I play on the bench the date in datetime2(7)?

  • Dates are not formatted. Just because they are shown in a format does not mean that they are necessarily saved in this format. Save the Date object and ready.

  • @user113924 If you have further questions, or if you will add more information, edit your question (do not add questions in the answers). And to save a date, you pass the object Date for setParameter (as I indicated above in my reply)

  • @user113924 Again: Do not edit my answer to add information regarding your question. Edit your question for this: put your attempts, inputs and outputs (what you got and what you would like to have gotten), etc. Read on help center how to make a [mcve]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.