问题
I want to insert the current timestamp as datetime data type into sql server using Scala.
So far I have tried this:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
var currentTimeStamp = LocalDateTime.now()
val datetimeFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm a")
var insertTimestamp = currentTimeStamp.format(datetimeFormat)
myInsertStatement.setInt(1, insertTimestamp)
This gives me insertTimestamp in the format "MM/dd/yyyy HH:mm a" but this is a string. Not datetime data type in sql.
How can I do this?
回答1:
Provided that you are using JDBC 4.2 or higher (which I believe that most of us are):
myInsertStatement.setObject(1, currentTimeStamp)
LocalDateTime
is a poor choice for a timestamp, though, because it doesn’t “know” its time zone and therefore is ambiguous as a point in time. While I don’t know SQL Server, for most database engines it would be better to use timestamp with timezone on the database side and OffsetDateTime
on the Scala side.
来源:https://stackoverflow.com/questions/56798914/scala-sql-server-how-to-insert-the-current-timestamp-as-datetime-in-sql-serve