2017-09-24 29 views
0

我有空間類型的組合框和到達和離開的兩個文本框, 我想要的是什麼時候到達= 2017-09-01 - 出發= 2017 -09-05已經存在,客戶將無法在同類型的房間選擇2017年9月2日至2017年9月5日如何在Java中迭代到達和離開日期,Mysql

String sql = "SELECT * FROM reservation WHERE room=? and arrival=? and 
departure=?"; 

try{ 
     pst=conn.prepareStatement(sql); 
     pst.setString(1, ComboBox.getSelectedItem().toString()); 
     pst.setString(2, txtArrival.getText()); 
     pst.setString(3, txtDeparture.getText()); 
     rs=pst.executeQuery(); 
if(rs.next()){ 
// the customer needs to choose other date or room 
} 
else{ 
// continue to fill up the form below 
} 
} 

enter image description here

+0

_「它不會接受它」_。請更具體地說明您的問題或您想要做的事情。 –

+0

我更新了它,對不起 – Francis

+0

你的意思是說,你有一個房間,而且這個房間可以預訂,數據庫存儲開始/結束。那麼如果其他人要預訂這個房間,並輸入開始/結束,那麼你想搜索房間是否可用? – forqzy

回答

0

存放在你的日期值日期數據類型的列,而不是文本。

使用適當的對象而不是單純的字符串。

LocalDate arrival = LocalDate.parse("2017-09-01") ; 

將該對象傳遞給您的PreparedStatement

myPreparedStatement.setObject(2 , arrival) ; 

而且對於ResultSet

LocalDate arrival = myResultSet.getObject(2 , LocalDate.class) ; 

修復SQL查詢的邏輯。你不應該在尋找確切的日期匹配。

所有這些已經在Stack Overflow上多次討論過了。發佈前徹底搜索。