简体   繁体   English

sql server jdbc查询datetime列

[英]sql server jdbc query on datetime column

I've looked around and could not seem to find this asked specifically, on SO, but I've found similar questions like this one and lots of questions regarding SQL itself or C#... 我环顾四周,并可能似乎无法找到这个特别要求,在左右,但我发现像类似的问题这一个关于SQL本身或C#以及大量的问题...

Here is what I am seeing: 这是我所看到的:

MapSqlParameterSource parameterSource = new MapSqlParameterSource();
        //parameterSource.addValue( "insertDate", DateTime.now().minusHours( 1 ).toGregorianCalendar(), Types.TIME );
        parameterSource.addValue( "insertDate", new Timestamp( DateTime.now().minusHours( 1 ).getMillis() ) );

        List< String > contents =
            _simpleJdbcTemplate
                .query(
                    "SELECT TOP (200) inserteddatetime, Contents FROM dbo.tsomeTable WHERE (ServiceName = 'something') and inserteddatetime > :insertDate",
                    new RowMapper< String >() {
                        @Override
                        public String mapRow( final ResultSet rs, final int rowNum ) throws SQLException {
                            System.out.println( rs.getTimestamp( "inserteddatetime" ) );

                            return rs.getString( "Contents" );
                        }
                    }, parameterSource );

The query "hangs"\\does nothing\\never returns if: 查询“挂起” \\不执行任何操作\\在以下情况下不会返回:

  1. I use the uncommented Timestamp object (as presented above) 我使用了未注释的Timestamp对象(如上所述)
  2. I replace the parameterSource object with DateTime.now().minusHours( 1 ).toGregorianCalendar() or DateTime.now().minusHours( 1 ).toGregorianCalendar().getTime() 我用DateTime.now().minusHours( 1 ).toGregorianCalendar()DateTime.now().minusHours( 1 ).toGregorianCalendar().getTime()替换parameterSource对象
  3. I try the commented out line, but change the type to Timestamp 我尝试注释掉的行,但是将类型更改为Timestamp

So, here is my question or questions... 所以,这是我的一个或多个问题...

Is there a known bug\\issue with querying on datetime columns in sql server? 在SQL Server中的datetime列上进行查询是否存在已知的bug \\问题?

Why do I have to use Time and not Timestamp ? 为什么我必须使用Time而不是Timestamp

I suspect that Spring is converting the date objects over to Timestamp when I query with the object directly (as demonstrated in #2). 我怀疑直接查询对象时,Spring将日期对象转换为Timestamp (如#2所示)。

  1. TIMESTAMP is a server-generated value used to help with data consistency , its not a simple data type, so for storing datetime value , avoid TIMESTAMP datatype. TIMESTAMP是服务器生成的值,用于帮助实现data consistency ,它不是简单的数据类型,因此为了storing datetime value ,请避免使用TIMESTAMP数据类型。

  2. Also, SQL Server TIMESTAMP is confusing , and it's deprecated . 另外,SQL Server TIMESTAMP confusing ,并且deprecated It is replaced by the ROWVERSION keyword , which should reduce confusion. ROWVERSION keyword代替,它可以减少混乱。

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

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