2016-12-30 75 views
0

我在我的項目中需要幫助..........此代碼顯示
單擊按鈕時出現'結果集關閉後不允許操作'錯誤消息... .................請幫助....謝謝結果集關閉後不允許操作 - 錯誤

private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {           
    String car_id = ""; 
    Date rent_date = null; 
    Date return_date = null; 
    Date rented_date = null; 

    String model = ""; 
    String rent_place = ""; 
    String return_place = ""; 
    double disrent = 0.0; 
    double fine = 0.0; 
    double rent = 0.0; 
    double totrent = 0.0; 
    int mem_id = Integer.parseInt(memidget.getText()); 


    try { 

     Class.forName("java.sql.DriverManager"); 
     Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/lion", "root", "achinth"); 
     Statement stmt = (Statement) con.createStatement(); 
     String query = "select * from car_use_log where returned='N' and mem_id=" + mem_id + " ;"; 
     ResultSet rs = stmt.executeQuery(query); 
     int a = 0; 

     while (rs.next()) { 
      car_id = rs.getString("car_id"); 
      rent_date = rs.getDate("rent_date"); 
      return_date = rs.getDate("return_date"); 
      rented_date = rs.getDate("rented_date"); 
      model = rs.getString("model"); 
      rent_place = rs.getString("place_of_rent"); 
      return_place = rs.getString("place_of_return"); 
      a++; 
     } 


     if (a == 0) { 
      JOptionPane.showMessageDialog(this, "You have currently not rented a car"); 
     } else { 
      curstatus.setVisible(true); 
      mem_login.setVisible(false); 
      t18.setText(model); 
      t19.setText(car_id); 
      t20.setText("" + rent_date); 
      t63.setText("" + rented_date); 
      t21.setText("" + return_date); 
      t24.setText(rent_place); 
      t25.setText(return_place); 

      String query1 = "select rent from car_details where model='" + model + "';"; 
      ResultSet rs1 = stmt.executeQuery(query1); 
      while (rs1.next()) { 
       rent = rs1.getDouble("rent"); 
      } 
      int no_days = 0; 
      Date curdate = null;//to initalise 
      int totno_days = 0; 
      int return_cur_diff = 0; 
      if (rented_date != null) { 

       String query2 = "select datediff('" + return_date + "',curdate()),datediff(curdate(),'" + rented_date + "'),datediff('" + return_date + "','" + rented_date + "'),curdate();"; 
       ResultSet rs2 = stmt.executeQuery(query2); 


       while (rs2.next()) { 
        no_days = rs2.getInt(2); 
        curdate = rs2.getDate(4); 
        return_cur_diff = rs.getInt(1); 
       } 


       if (return_cur_diff < 0) { //Car is not returned after return date ....... fine calulation needed 
        disrent = totno_days * rent; 
        fine = -2 * return_cur_diff * rent; 
        totrent = fine + disrent; 
       } 
       else if (return_cur_diff > 0) { 
        disrent = no_days * rent; 
        jLabel39.setText("Rent to be Paid on Return Date :"); 
        totrent = disrent; 
       } 
       else { 
        disrent = totno_days * rent; 
        totrent = disrent; 
       } 

      } 
      } 
      t22.setText("" + disrent); 
      t23.setText("" + fine); 
      t26.setText("" + totrent); 




    } catch (Exception e) { 
     JOptionPane.showMessageDialog(this, e.getMessage()); 
    } 

}       

回答

1

其實,問題就在這裏:

if (rented_date != null) { 

       String query2 = "select datediff('" + return_date + "',curdate()),datediff(curdate(),'" + rented_date + "'),datediff('" + return_date + "','" + rented_date + "'),curdate();"; 
       ResultSet rs2 = stmt.executeQuery(query2); 


       while (rs2.next()) { 
        no_days = rs2.getInt(2); 
        curdate = rs2.getDate(4); 
        return_cur_diff = rs.getInt(1); 
       } 

從Oracle文檔(http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html):

當生成它的Statement對象 關閉,重新執行或用於從多個結果序列中檢索下一個 結果時,ResultSet對象會自動關閉。

您正在重複使用導致關閉rs對象的stmt對象;但在重複使用stmt對象後,它在while循環中使用。順便說一句,我相信這只是一個錯字。

+0

我應該如何糾正它?我應該創建一個更多的聲明? –

+0

正如我在回答中提到的,我相信你剛剛犯了一個錯字。正如我可以從變量名稱中看到的那樣,爲了解決這個錯誤,您必須用'return_cur_diff = rs2.getInt(1);'替換'return_cur_diff = rs.getInt(1);'。 – arcquim

+0

非常感謝! –

相關問題