4

さて、私は現在、次の時間を持つデータベースを持っています:

ID 1 startTime 2013-09-09 15:05:10.0 endTime 2013-09-09 15:05:10.0

ID 2 startTime 2013-09-09 15:09:54.0 endTime 2013-09-09 15:09:54.0

ID 3 startTime 2013-09-09 15:20:46.0 endTime 2013-09-09 15:20:46.0

id 4 startTime 2013-09-09 15:21:06.0 endTime 2013-09-09 15:21:06.0

id 5 startTime 2013-09-09 15:21:34.0 endTime 2013-09-09 15:21:34.0

ID 6 startTime 2013-09-09 15:22:34.0 endTime 2013-09-09 15:22:34.0

id 7 startTime 2013-09-09 15:23:06.0 endTime 2013-09-09 15:25:34.0

今、ここにある時間メソッドで検索を実行すると:

@Override
public ArrayList<AppointmentAccess> searchByTime(Timestamp startTime,
        Timestamp endTime) throws SQLException {
    ArrayList<AppointmentAccess> appointmentList = new ArrayList<AppointmentAccess>();

    String preparedQuery = "Select DISTINCT * From Appointments where startTime <= appointments.endTime AND endTime >= appointments.startTime";

    // Connect to database
    try (Connection connection = DriverManager.getConnection(url, user,
            password);

    // Run SQL
    PreparedStatement ps = connection.prepareStatement(preparedQuery);

    // Get SQL results
    ResultSet query = ps.executeQuery();) {

        while (query.next()) {
            AppointmentAccess appointment = new AppointmentAccess();
            appointment.setStartTime(query.getTimestamp("starttime"));
            appointment.setEndTime(query.getTimestamp("endtime"));

                appointment.setAlarmReminder(query
                        .getBoolean("alarmreminder"));
                appointment.setAllDay(query.getBoolean("allday"));
                appointment.setDetails(query.getString("details"));
                appointment.setLocation(query.getString("location"));
                appointment.setTitle(query.getString("title"));
                appointmentList.add(appointment);
        }
    }
    // Returns a List of all the contacts
    return appointmentList;
}

テスト メソッド「searchTooLate、searchTooEarly、searchTimeBetweenAppointments」が失敗し続けます。これらのメソッドを送信する時間は次のとおりです。

startTime: "2013-09-09 16:05:09" endTime: "2013-09-09 16:22:35"

startTime: "2013-09-09 15:24:06.0" endTime: "2013-09-09 15:25:30.0"

startTime:"2013-08-09 14:05:09" endTime:"2013-08-09 16:22:35"

私は何を間違っていますか!?

4

1 に答える 1

0

コードの次の部分を検討してください。String preparedQuery = "Select DISTINCT * From Appointments where startTime <= appointments.endTime AND endTime >= appointments.startTime";

どのように変化することを提案しますか? 毎回 startTime を "Appointments.editTime" と比較しています (意図的に二重引用符で囲んでいます)。Appointments.editTime はデータベース内の変数ではないため、クエリが失敗する原因となります。

意図したのかもしれません..." + Appointments.startTime.ToString() + "...が、これはまだ間違った方法ですが、これによりコードが機能します。PROC とパラメーターを検討してください。

于 2013-10-07T18:29:30.590 に答える