我在通过 java 使用 Oracle 时从我的数据库中获取日期时遇到问题。如果我在 Java 中使用它:
SELECT *
from HA2_BOOKINGS
WHERE ROOM like 'R1'
AND BEGINNING >= (TO_TIMESTAMP('2018-06-11 15:11:43.208', 'YYYY-MM-DD HH24:MI:SSXFF'))
AND END <= (TO_TIMESTAMP('2018-06-11 15:11:43.208', 'YYYY-MM-DD HH24:MI:SSXFF'))
我收到 oracle-01830 错误,但是当我在 oracle 本身中使用完全相同的东西时,代码可以工作。有谁知道为什么?
我的java代码:
public Collection<Booking> getAllBookingsForRoomInPeriod(Room r, String startDate, String endDate) {
Collection<Booking> bookingsForRoomInPeriod = new ArrayList<Booking>();
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm");
parser.setLenient(false);
Date dateBeginning = new Date();
Date dateEnd = new Date();
try {
dateBeginning = parser.parse(startDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dateEnd = parser.parse(endDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Timestamp tsBeginning = new Timestamp(dateBeginning.getTime());
Timestamp tsEnd = new Timestamp(dateEnd.getTime());
bookingList.clear();
Connection aCon = Persistence.getConnection();
ResultSet resultSet = null;
// Raeume lesen
try {
resultSet = Persistence.executeQueryStatement(aCon, "SELECT * from HA2_BOOKINGS WHERE ROOM like '" + r.getRoomNr() + "' AND BEGINNING >= (TO_TIMESTAMP('" + tsBeginning + "', 'YYYY-MM-DD HH24:MI')) AND END <= (TO_TIMESTAMP('" + tsEnd + "', 'YYYY-MM-DD HH24:MI'))");
while (resultSet.next()) {
Booking a = new Booking();
a.setRoom(resultSet.getString("ROOM"));
a.setUsername(resultSet.getString("USERNAME"));
a.setDescription(resultSet.getString("DESCRIPTION"));
a.setBeginning(resultSet.getTimestamp("BEGINNING"));
a.setEnd(resultSet.getTimestamp("END"));
this.bookingList.add(a);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Persistence.closeConnection(aCon);
}
System.out.println(bookingList);
return bookingList;
}