Java和MySQL查询日期

时间:2015-07-10 13:47:11

标签: java mysql date

我的Java代码是:

var app = angular.module('prime', ['ngSanitize','ngResource','angular-loading-bar','ngAnimate','ui.bootstrap'],  
 function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
});


app.factory('JsonService', function ($resource,model) {
    return $resource('/api/users/?page=:page',{page: "@page"});
});

app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start;
            return input.slice(start);
        }
        return [];
    };
});

app.controller('ResultsController', function($scope, $http, JsonService, filterFilter) {

    $scope.results = [];
    $scope.search = [];
    $scope.currentPage = 1;
    $scope.entryLimit = 10; // itens por pagina

    $scope.init = function() {
        JsonService.get({}, function (response) {
            $scope.results = response.data;
            $scope.totalItems = response.total;
            $scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit);           
        }, function (error) {
            $scope.results = [];
        });
    };

    $scope.resetFilters = function () {
        $scope.search = [];
    };

    // runs $watch on the search field
    $scope.$watch('search', function (newVal, oldVal) {
        $scope.filtered = filterFilter($scope.results, newVal);
        $scope.totalItems = $scope.filtered.length;
        $scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit);
        $scope.currentPage = 1;
    }, true);

    $scope.$watch('currentPage', function(newPage) {
        $scope.init($scope.numberPage);
    });  

});

我的功能是控制用户输入的两个日期,并将它们与数据库进行比较。如果日期在时间范围内,你应该给我回id_rent。 但我不明白为什么不能进入while循环:

public void FillCombo_Disp(){
    java.sql.Date fromDate = new java.sql.Date(jDateChooser_Date1.getDate().getTime());
    java.sql.Date toDate= new java.sql.Date(jDateChooser_Date2.getDate().getTime());
    String space = jComboBox_Space.getSelectedItem().toString();
    String [] space_split = space.split(" ");
    String id_space = space_split[0];
    String query = "SELECT ID FROM Rent WHERE ID_Space = '" + id_space + "' AND Date1 BETWEEN ? AND ? OR Dat2 BETWEEN ? AND ?";
    try {
        pst = conn.Connection().prepareStatement(query);
        pst.setDate(1, fromDate);
        pst.setDate(2, toDate);
        pst.setDate(3, fromDate);
        pst.setDate(4, toDate);

        ResultSet res = pst.executeQuery();
        int ID = 0;
        while(res.next()) {
            ID = res.getInt("ID");
        }

        if(ID!=0)
            jTextField_Disp.setText("Rented");
        else
            jTextField_Disp.setText("Free");
    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

帮助我!

1 个答案:

答案 0 :(得分:2)

您在查询中犯了拼写错误,因此它不返回任何行。你把这个词放在" Dat2"当我相信它应该是" Date2"。

电流:

String query = "SELECT ID FROM Rent WHERE ID_Space = '" + id_space + "' AND Date1 BETWEEN ? AND ? OR Dat2 BETWEEN ? AND ?";

新:

String query = "SELECT ID FROM Rent WHERE ID_Space = '" + id_space + "' AND Date1 BETWEEN ? AND ? OR Date2 BETWEEN ? AND ?";