2017-04-30 40 views
0

在我爲我的oracle數據庫中的uni.Im數據從一個表中獲取數據時,一個奇怪的select查詢行爲出現在我的路上。日期列上的選擇查詢的奇怪行爲

selectString = "select * from reservation"; 
prestatement = dbConnection.prepareStatement(selectString); 
rs = prestatement.executeQuery(selectString); 
while (rs.next()) { 
    String rdate = rs.getString("reservdate").substring(0, 10); 
    jComboBox1.addItem(rdate); 
//.... 
//....etc.. 

問題是,什麼是顯示在我的組合框是覺得像「1999年10月10日」 之後,我要拉一些數據,其中我必須與選定的日期選擇那些項目在組合框上。好的,這是我的問題。

String x = String.valueOf(jComboBox1.getSelectedItem()); 
selectString="select * from reservation where reservdate='"+x+"'"; 
//...etc.. 

後,我運行即時得到SQL異常與消息:消息:ORA-01861:文字不匹配格式字符串 我搜索一點點網頁,發現,如果我運行這個選擇查詢一切正常細

selectString="select * from reservation where reservdate='10-OCT-99'"; 

所以我的問題是,什麼是讓這個幹活意味着我應該嘗試編輯所有日期的組合框格式的最佳方式?或者我一直在做錯誤的事情,應該改變這種情況?

在此先感謝。

回答

0

您可以:

1-重寫你的相關類的(無論對象getSelectedItem回報,在這種特殊情況下它已經是一個字符串,你可能不需要那麼將String.valueOf()調用)的toString方法實現你所需要的格式。 (壞樣做)

2 - 讓Oracle數據庫以其TO_DATE功能(一個更好的做法的一種辦)處理它

「TO_DATE(yourDateString,日期格式)」

String date = String.valueOf(jComboBox1.getSelectedItem()); 
selectString="select * from reservation where reservdate= TO_DATE('" + date + "','DD-MON-YY')"; 

並防止SQL注入,使用帶有一個PreparedStatement後者的做法是這樣的:

String date = String.valueOf(jComboBox1.getSelectedItem());  
String selectString="select * from reservation where reservdate= TO_DATE(?,'DD-MON-YY')"; 
PreparedStatement preStatement = dbConnection.prepareStatement(selectString); 
preStatement.setString(1,date); 
ResultSet rs = preStatement.executeQuery(); 

Official Docs

+0

即時通訊:消息:ORA-01756:引用的字符串未正確終止。 –

+0

現在我回到了我開始的地方:消息:ORA-01861:文字不匹配格式字符串 –

+0

同樣的事情..我也試過mm代替週一,但仍然不能正確地得到它。 –