2016-04-05 65 views
0

我編寫的程序檢查酒店中可用的房間。我使用JCalendar爲用戶輸入日期和JTable來顯示可用的房間。 問題是,當我點擊檢查可用性按鈕時,它拋出一個SQL異常。使用JTable和JCalendar檢查房間可用性拋出SQL異常

以下是代碼片段:

public class Room_Availability extends JFrame { 

//Note: Didn't add codes about instantiating panels, setting layouts and all 

JLabel arr_date= new JLabel("Arrival Date:"); 
JLabel dep_date= new JLabel("Departure Date:"); 
JButton chk_btn= new JButton("Check Availability"); 
JDateChooser arrival = new JDateChooser(); 
JDateChooser departure = new JDateChooser(); 
JTable CheckAvail= new JTable(); 

Room_Availability(){ 

chk_btn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      CheckAvailActionPerformed(evt); 
     } 
    }); 

    } 


private void CheckAvailActionPerformed(java.awt.event.ActionEvent evt) {            


    ResultSet rs = null; 
    Connection conn = null; 
    Statement stmt = null; 
    try { 
     // new com.mysql.jdbc.Driver(); 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     String connectionUrl = "jdbc:mysql://localhost:3306/testing?autoReconnect=true&useSSL=false"; 
     String connectionUser = "root"; 
     String connectionPassword = "admin"; 
     conn = DriverManager.getConnection(connectionUrl, connectionUser, 
       connectionPassword); 
     stmt = conn.createStatement(); 

     String query= "select * from room as ro" + 
       "where ro.room_id not in "+ 
       " ("+ 
        "select re.room_no"+ 
        " from testing.booking as re "+ 
        "where (arrival_date >= ? and departure_date < ?)"+ 
        "or (departure_date >= ? and arrival_date < ?)"+ 
       ")"; 

     PreparedStatement pStmt = (PreparedStatement) conn.prepareStatement(query); 

     java.util.Date utilDate = arrival.getDate(); 
     java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); 

     java.util.Date utilDate1 = departure.getDate(); 
     java.sql.Date sqlDate1 = new java.sql.Date(utilDate1.getTime()); 

     pStmt.setDate(1, sqlDate); 
     pStmt.setDate(2, sqlDate1); 
     pStmt.setDate(3, sqlDate); 
     pStmt.setDate(4, sqlDate1); 


     rs = pStmt.executeQuery(); 

     CheckAvail.setModel(DbUtils.resultSetToTableModel(rs)); 


    } catch (Exception e) { 
     e.printStackTrace(); 
    }  
    } 
} 

這裏是GUI的樣子: enter image description here

當我執行查詢(具有相同的日期,因爲我在JCalendar進入)上的MySQL數據庫這是什麼顯示和應顯示在JTable上: enter image description here

這是我得到的錯誤:

enter image description here

+3

有4個問題n在你的查詢中標記,這意味着它需要4個參數;你只能指定2個參數。 –

+0

我註釋掉了''或(departure_date> =?和arrival_date <?)「+'',但現在我得到'MySQLSyntaxErrorException' – Tia

回答

2

我想你應該爲每個'?'設置參數。在查詢中。有四個 '?'佔位符,但您可以設置只有兩個

pStmt.setDate(1, sqlDate); 
    pStmt.setDate(2, sqlDate1); 

應該是這樣的

pStmt.setDate(1, sqlDate); 
pStmt.setDate(2, sqlDate1); 
pStmt.setDate(3, sqlDate1); 
pStmt.setDate(4, sqlDate); 
+0

謝謝,我只是試過了,但它拋出了'MySQLSyntaxErrorException' – Tia

+0

@Diksha,你能編輯你的questiowith試圖根據這個答案解決它後的代碼? –

+0

@LajosArpad完成! – Tia

1

有缺失空間線:

"where (arrival_date >= ? and departure_date < ?)"+ 
"select * from room as ro" + 

就在結尾處添加一個空格字符串

"where (arrival_date >= ? and departure_date < ?) "+ 
"select * from room as ro " + 
+0

它仍然拋出'MySQLSyntaxErrorException' – Tia

+0

我編輯了我的答案,還有一行沒有空格,最好檢查一下 –

+0

非常感謝!有效! – Tia