2011-10-05 66 views
1

後,我有這樣的一段代碼:值java.sql.SQLException:不允許操作的ResultSet關閉

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    String Username2 = request.getParameter("Username2"); 
    String Password2 = request.getParameter("Password2"); 
    String ResetPassword = request.getParameter("ResetPassword"); 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     String st = "jdbc:mysql://localhost:3306/LoginAccount"; 
     Connection conn = (Connection) DriverManager.getConnection(st, "root", "baljinder"); 
     Statement sta = (Statement) conn.createStatement(); 
     ResultSet rs = sta.executeQuery("SELECT * FROM Account where Username='" + Username2 + "' && Password ='" + Password2 + "' ;"); 

     while (rs.next()) { 

      if (Username2.equals(rs.getString("Username")) && Password2.equals(rs.getString("Password"))) { 
       sta.executeUpdate("update Account set Password ='" + ResetPassword + "' where Username='" + Username2 + "' ;"); 
       out.print("your successful to Reset the password"); 
       conn.close(); 
      } else { 
       out.println("<h1>the Username and Password didn't match did not found </h1>"); 
      } 
     } 
    } catch (SQLException ex) { 
     Logger.getLogger(AccountServlet.class.getName()).log(Level.SEVERE, null, ex); 
     //out.print(ex); 
    } catch (ClassNotFoundException ex) { 
     Logger.getLogger(AccountServlet.class.getName()).log(Level.SEVERE, null, ex); 
     out.print(ex); 
    } finally { 
     out.close(); 
    } 
} 

我又再次得到這個&,

值java.sql.SQLException: ResultSet關閉後不允許操作

有什麼錯誤?

+0

哪一行是拋出異常? – Pat

+0

@Pat while(rs.next()) – user981098

+5

Btw ...令人愉快的SQL注入攻擊你正在創建。你在創建什麼網站,以便世界可以輕鬆攻擊它? – Pat

回答

6

您正在關閉數據庫連接,同時遍歷ResultSet。 A ResultSet需要打開它的連接才能工作。

您還應該確保在finally塊中關閉JDBC連接。

+1

如果用戶名在數據庫中是嚴格唯一的,那麼'while'最好是'if'。它也可以解決問題,但它絕對不是正確的方法。資源關閉應始終以「終於」的方式進行。 – BalusC

相關問題