英文:
Does this java parameter would insert a row into sql server?
问题
我有一段Java代码,我想在SQL Server上测试它是否会插入一行数据。目前,我还没有测试这段代码的工具。以下是代码部分:
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("id", "1"); // 主键
parameters.addValue("date", new GregorianCalendar(TimeZone.getTimeZone("America/New_York")).getInstance().getTimeInMillis()); // 日期的 Calendar 对象
return parameters;
而SQL Server的表将会类似于:有一个 INTEGER 类型的 id 和一个 DATETIME2 类型的 date(必须是 DATETIME2)。
我想知道这段代码是否会插入一行数据。
我遇到的问题是:我有这段代码,它目前在Oracle数据库上可以工作。我正在使用HSQLDB在单元测试中通过创建具有相同列和数据类型的重复表来测试它。现在,我需要将数据库从Oracle迁移到SQL Server。作为一个过程,我正在使项目在Oracle和SQL Server上兼容,而不编写单独的代码。
在我的新单元测试中,我使用HSQLDB来创建表和列。由于某种原因,当将此Calendar
类型作为参数传入时,HSQLDB抛出org.hsqldb.HsqlException: data exception: invalid datetime format
错误。我想确认这个问题是来自HSQLDB还是SQL Server。
英文:
I have Java code that I want to test on SQL server if it would insert a row. Currently, I don't have a tool to test this code yet. Here is the code:
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("id", "1"); // primary key
parameters.addValue("date", new GregorianCalendar(TimeZone.getTimeZone("America/New_York").getInstance().getTimeInMillis()); // Calendar object for date
return parameters;
and SQL Server table would be something like id with INTEGER and date with DATETIME2 (this must be DATETIME2).
I would like to know if this would insert a row.
Problem I'm having: I have this code which currently works with Oracle database. I am using HSQLDB to test it on my unit test by creating duplicate table with same column and data type. Now, I need to migrate database from Oracle to SQL Server. As a process, I am making the project compatible for both Oracle and SQL Server without writing separate code.
In my new unit test, I use HSQLDB to create table and columns. For some reason, HSQLDB throws org.hsqldb.HsqlException: data exception: invalid datetime format
error when this Calendar
type is passed in as parameter. I want to make sure if this problem is from HSQLDB or SQL Server.
答案1
得分: 1
这是一个与JDBC驱动程序相关的问题,但我不会过分责怪它;Calendar是一个糟糕的API,而且已经过时了。不要使用它。
发送的“最安全”日期/时间类型是java.sql.Timestamp
的实例。发送的“最佳”日期/时间类型是java.time
包中的某个内容,最好是ZonedDateTime
。如果您的JDBC驱动程序允许您这样做,就这样做。如果不允许,抱怨一下,咬紧牙关,向JDBC驱动程序制造商发送错误报告,并使用java.sql.Timestamp
。
英文:
It's a problem with the JDBC driver, but I wouldn't blame it overly much; Calendar is horrible API, and obsolete to boot. Don't use it.
The 'safest' date/time type to send is an instance of java.sql.Timestamp
. The 'best' date/time type to send is something from the java.time
package, preferably a ZonedDateTime
. If your JDBC driver lets you do that, do that. If it does not, grumble, grit your teeth, fire off a bug report with the JDBC driver maker, and use java.sql.Timestamp
.
答案2
得分: 1
你的代码尝试将一个 Long
值赋值给一个(JDBC)TIMESTAMP
列。在 JDBC 中,将 long
(或 Long
)值设置到 TIMESTAMP
列是没有定义的。如果在 Oracle 中有效,那是 Oracle 驱动程序特定的非标准扩展,因此不能保证在其他驱动程序中有效。
你需要使用 JDBC 定义的类型。对于 TIMESTAMP
(不带时区)列,JDBC 4.3 规范 只定义支持以下 Java 类型:
-
java.sql.Timestamp
(主要类型,但建议改用java.time.LocalDateTime
) -
java.lang.String
-
java.sql.Date
(建议改用java.time.LocalDate
) -
java.sql.Time
(建议改用java.time.LocalTime
) -
java.util.Calendar
(虽然我不确定这是否常常受支持) -
java.util.Date
(虽然我不确定这是否常常受支持) -
java.time.LocalDate
(在 JDBC 4.2 中引入) -
java.time.LocalTime
(在 JDBC 4.2 中引入) -
java.time.LocalDateTime
(在 JDBC 4.2 中引入)
英文:
Your code tries to assign a Long
value to a (JDBC) TIMESTAMP
column. Setting a long
(or Long
) value on a TIMESTAMP
column is not defined in JDBC. If this works in Oracle, then that is a non-standard extension specific to the Oracle driver, and therefor is not guaranteed to work in other drivers.
You will need to use the types that are defined by JDBC. For a TIMESTAMP
(without time zone) column, the JDBC 4.3 specification only defines support for the following Java types:
java.sql.Timestamp
(primary type, but recommendation is to usejava.time.LocalDateTime
instead)java.lang.String
java.sql.Date
(recommendation is to usejava.time.LocalDate
instead)java.sql.Time
(recommendation is to usejava.time.LocalTime
instead)java.util.Calendar
(though I'm not sure if this is commonly supported)java.util.Date
(though I'm not sure if this is commonly supported)java.time.LocalDate
(introduced in JDBC 4.2)java.time.LocalTime
(introduced in JDBC 4.2)java.time.LocalDateTime
(introduced in JDBC 4.2)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论