简体   繁体   English

使用JDBC将String作为datetime2(0)插入SQL Server 2012会导致错误

[英]Inserting String as datetime2(0) into SQL Server 2012 using JDBC causes error

I gather data from an external source and wish to insert it into my SQL Server 2012 database. 我从外部来源收集数据,并希望将其插入我的SQL Server 2012数据库中。 When I attempt to insert a String, formatted as follows: "YYYY-MM-DD hh:mm:ss", which complies with the documentation ., it doesn't go quite as I want to. 当我尝试插入一个字符串时,其格式如下:“ YYYY-MM-DD hh:mm:ss”,它符合文档 。,但并没有达到我想要的效果。

For clarification, the first column of my table is of type datetime2(0). 为了澄清起见,我表的第一列的类型为datetime2(0)。

Upon executing the following code 执行以下代码后

insertData() {
...

insert.execute("INSERT INTO [LindTrading].[dbo].[Bloomberg]" +
                " VALUES" +
                "(" +
                trade.getTimestamp() + " ," +
                trade.getAmount() + "," +
                trade.getPrice() + "," +
                trade.getTicker() +
                ")");

...
}

An example of a timestamp is "2014-07-22 00:04:08". 时间戳的示例是“ 2014-07-22 00:04:08”。

I get the following error: (...).SQLServerException: Incorrect syntax near '00'. 我收到以下错误:(...)。SQLServerException:'00'附近的语法不正确。

Also, getTicker() returns a string such as "CF US EQUITY", and the spaces surrounding US seem to cause similar problems, when I attempt to insert into a varchar(25) column. 另外,当我尝试插入varchar(25)列时,getTicker()返回一个字符串,例如“ CF US EQUITY”,并且US周围的空格似乎引起类似的问题。

What am I doing wrong? 我究竟做错了什么?

The string containing the datetime should be within apostrophes, like 包含日期时间的字符串应在撇号内,例如

'2014-07-23 12:00:00' '2014-07-23 12:00:00'

And not only 不仅

2014-07-23 12:00:00 2014-07-23 12:00:00

Is this the case? 是这样吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM