Scala, SQL Server - How to insert the current timestamp as datetime in SQL server using Scala?

删除回忆录丶 提交于 2019-12-11 12:50:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!