尝试按日期删除时,查询不会生成结果集。

时间:2018-05-03 18:54:55

标签: java sql prepared-statement

我正在尝试在java中创建一个方法,该方法将在某个日期之前删除数据库中的所有记录。现在我得到这个SQLException:查询或过程不返回结果集。

这是我的单元测试用于填充数据库的插入语句之一。

INSERT INTO C35_6 ( xIndex, xcSource, dTimeOn, dTimeOff, bSuccessful ) 
VALUES (74013, 1, '05/01/1972 00:00:00.000', '05/02/1972 00:00:00.000', 1 )

这是删除声明

DELETE FROM C35_6 WHERE dTimeOff <= '12/31/1975 00:00:00.000'

我可以在日志文件中看到这些语句以正确的顺序运行,一个接一个。这些语句是使用setDate()使用java preparedStatement创建的。

似乎sql由于某种原因无法比较这些日期。

2 个答案:

答案 0 :(得分:3)

我认为您设定日期这一事实无关紧要。异常消息表明&#34;查询不返回结果集&#34;。这是真的。您的两个查询都不会返回结果。

这意味着你以错误的方式执行它们。我的猜测是你用preparedStatement.executeQuery()运行它们(用于SELECT查询并期望返回ResultSet)而不是preparedStatement.execute(),可用于任何类型的查询

答案 1 :(得分:0)

Answer by Elevate对于您的例外与日期时间处理无关的原因是正确的。

您的日期时间处理存在问题。

  • 忽略时区的关键问题。
  • 使用哑字符串代替智能对象。

以下是使用H2 Database的完整示例。

ZonedDateTime对象代表时刻,即时间线上的一个点,通过特定区域的人(time zone)使用的挂钟时间来看。

我们只需从UTC中提取Instant,就可以在ZonedDateTime的挂钟时间内查看同一时刻,时间轴上的同一点。 H2数据库的JDBC驱动程序需要Instant,因为其PreparedStatement::setObject方法似乎无法直接处理ZonedDateTime。大多数数据库(包括H2)在UTC中存储TIMESTAMP WITH TIME ZONE。有些人会将来自其他时区的传入值调整为UTC,但显然H2需要我们进行调整。

package com.basilbourque.example;

import org.h2.jdbcx.JdbcDataSource;

import java.sql.*;
import java.time.*;

public class App {
    public static void main ( String[] args ) {
        App app = new App();
        app.doIt();
    }

    private void doIt () {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL( "jdbc:h2:mem:bogus;DATABASE_TO_UPPER=FALSE;DB_CLOSE_DELAY=-1" );  // Create in-memory database that disappears when this JVM exits.
        ds.setUser( "sa" );  // Arbitrary.
        ds.setPassword( "sa" );

        ZoneId z = ZoneId.of( "America/Montreal" ); // Arbitrarily chosen time zone.

        // Define "event" table with 3 columns: id number auto-generated, beginning of event, ending of event.
        String sql = "CREATE TABLE event_  ( id_ INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY , start_ TIMESTAMP WITH TIME ZONE NOT NULL, stop_ TIMESTAMP WITH TIME ZONE NOT NULL ) ;";
        try (
                Connection conn = ds.getConnection() ;
                Statement stmt = conn.createStatement() ;
        ) {
            stmt.executeUpdate( sql );
        } catch ( SQLException e ) {
            e.printStackTrace();
        }

        // Insert rows for years 1972, 1977, & 1973.
        sql = "INSERT INTO event_ (  start_ , stop_ ) VALUES ( ? , ? ) ; ";
        try (
                Connection conn = ds.getConnection() ;
                PreparedStatement ps = conn.prepareStatement( sql ) ;
        ) {
            ps.setObject( 1 , LocalDate.of( 1972 , Month.MAY , 1 ).atStartOfDay( z ).toInstant() );
            ps.setObject( 2 , LocalDate.of( 1972 , Month.MAY , 2 ).atStartOfDay( z ).toInstant() );
            ps.executeUpdate();

            ps.setObject( 1 , LocalDate.of( 1977 , Month.JUNE , 1 ).atStartOfDay( z ).toInstant() );
            ps.setObject( 2 , LocalDate.of( 1977 , Month.JULY , 2 ).atStartOfDay( z ).toInstant() );
            ps.executeUpdate();

            ps.setObject( 1 , LocalDate.of( 1973 , Month.AUGUST , 1 ).atStartOfDay( z ).toInstant() );
            ps.setObject( 2 , LocalDate.of( 1973 , Month.AUGUST , 2 ).atStartOfDay( z ).toInstant() );
            ps.executeUpdate();


        } catch ( SQLException e ) {
            e.printStackTrace();
        }


        // Dump table.
        System.out.println( "Dumping event_ table. " );
        sql = "SELECT * FROM event_  ; ";
        try (
                Connection conn = ds.getConnection() ;
                PreparedStatement ps = conn.prepareStatement( sql ) ;
                ResultSet rs = ps.executeQuery() ;
        ) {
            while ( rs.next() ) {
                System.out.println( "id_:  " + rs.getInt( 1 ) + " | " + rs.getObject( 2 , Instant.class ).atZone( z ) + " to " + rs.getObject( 3 , Instant.class ).atZone( z ) );
            }
        } catch ( SQLException e ) {
            e.printStackTrace();
        }

        // Deleting rows for events starting before beginning of 1976. Should delete 2 of our 3 rows, leaving 1 row.
        System.out.println( "Deleting rows. " );
        sql = "DELETE FROM event_ WHERE start_ < ? ; ";
        try (
                Connection conn = ds.getConnection() ;
                PreparedStatement ps = conn.prepareStatement( sql ) ;
        ) {
            ps.setObject( 1 , Year.of( 1976 ).atDay( 1 ).atStartOfDay( z ).toInstant() );  // First moment of 1976 in our time zone.
            ps.executeUpdate();
        } catch ( SQLException e ) {
            e.printStackTrace();
        }

        // Dump table.
        System.out.println( "Dumping event_ table. " );
        sql = "SELECT * FROM event_  ; ";
        try (
                Connection conn = ds.getConnection() ;
                PreparedStatement ps = conn.prepareStatement( sql ) ;
                ResultSet rs = ps.executeQuery() ;
        ) {
            while ( rs.next() ) {
                System.out.println( "id_:  " + rs.getInt( 1 ) + " | " + rs.getObject( 2 , Instant.class ).atZone( z ) + " to " + rs.getObject( 3 , Instant.class ).atZone( z ) );
            }
        } catch ( SQLException e ) {
            e.printStackTrace();
        }


    }
}

跑步时。

  

转储event_表。

     

id_:1 | 1972-05-01T00:00-04:00 [美国/蒙特利尔]至1972-05-02T00:00-04:00 [美国/蒙特利尔]

     

id_:2 | 1977-06-01T00:00-04:00 [美国/蒙特利尔]至1977-07-02T00:00-04:00 [美国/蒙特利尔]

     

id_:3 | 1973-08-01T00:00-04:00 [美国/蒙特利尔]至1973-08-02T00:00-04:00 [美国/蒙特利尔]

     

删除行。

     

转储event_表。

     

id_:2 | 1977-06-01T00:00-04:00 [美国/蒙特利尔]至1977-07-02T00:00-04:00 [美国/蒙特利尔]

关于 java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和&amp; SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore