Scala,SQL Server-如何使用Scala在SQL Server中将当前时间戳作为日期时间插入? [英] Scala, SQL Server - How to insert the current timestamp as datetime in SQL server using Scala?

查看:216
本文介绍了Scala,SQL Server-如何使用Scala在SQL Server中将当前时间戳作为日期时间插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Scala将当前时间戳作为datetime数据类型插入到sql server中.

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)

这使我可以使用"MM/dd/yyyy HH:mm a"格式的insertTimestamp,但这是一个字符串.不是SQL中的datetime数据类型.

This gives me insertTimestamp in the format "MM/dd/yyyy HH:mm a" but this is a string. Not datetime data type in sql.

我该怎么做?

推荐答案

提供您使用的是JDBC 4.2或更高版本(我相信我们大多数人都在使用):

Provided that you are using JDBC 4.2 or higher (which I believe that most of us are):

myInsertStatement.setObject(1, currentTimeStamp)

但是,

LocalDateTime对于时间戳来说是一个糟糕的选择,因为它不知道"其时区,因此作为时间点是模棱两可的.尽管我不了解SQL Server,但对于大多数数据库引擎而言,最好将时间戳记与时区一起使用在数据库方面,而OffsetDateTime在Scala方面.

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.

这篇关于Scala,SQL Server-如何使用Scala在SQL Server中将当前时间戳作为日期时间插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆