2014-05-05 51 views
-1

當我輸入正確的用戶名/密碼時,查詢正常工作,客戶端與服務器之間的連接正常工作。但是,如果我做了不正確的用戶名/密碼組合,那麼程序會凍結正確的一個。我使用JDBC連接使用Java。在多個查詢條目後,JDBC會凍結

String usr = clientName.getText(); 
      String pass = clientPass.getText(); 
      String u = null, p = null; 

      ResultSet results=null; 

      String stmtLogin = "Select * from userInfo where username = '" + usr + "'"; 

      try 
      {   
       results = statement.executeQuery(stmtLogin); 

       results.next();   
        u = results.getString(1); 
        p = results.getString(2);   


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

      if ((pass.equals(p)) && usr.equals(u)) 

      { 
       message = clientName.getText(); 
       output.println(message);    



       btnSendM.setEnabled(true); 
       btnConn.setEnabled(false); 
       clientName.setEditable(false); 
       clientPass.setEditable(false); 

       messageBox.append(message + " has entered the chatroom \n"); 
       btnSendM.setFont(new Font("Verdana", Font.BOLD, 10)); 
       updateConnList(); 
      } 
      else 
       JOptionPane.showMessageDialog(null, "Username Password Incorrect, Please try Again"); 
+0

你嘗試過使用調試器嗎? – akhilless

+1

你有沒有關閉這些東西? 「ResultSet?''Statement?''Connection?' – EJP

+0

不,我不會在這裏關閉它們,生病嘗試關閉它們,看看是否有幫助。謝謝 – user3125462

回答

0

與您的不完全相同,但這是我登錄我的應用程序之一。

您的問題可能是您完成後沒有關閉connection

try { 
       String dbName = "ServerSQLite.db"; 
       connection = DriverManager.getConnection("jdbc:sqlite:" + "serverDatabase/" + dbName); 
       Statement statement = connection.createStatement(); 
       statement.setQueryTimeout(20); // set timeout to 20 sec. 

       ResultSet rs = statement.executeQuery("select * from registratedClients WHERE clientLogin = " + "\'" + loginName + "\'" +"and clientPassword = " + "\'" + password + "\'"); 
       //Do stuff with resultset 
       rs.close(); 


      } catch (Exception e) { 
       // if the error message is "out of memory", 
       // it probably means no database file is found 
       System.err.println(e.getMessage()); 
      } finally { 
       try { 
        if (connection != null) 
         connection.close(); 
       } catch (SQLException e) { 
        // connection close failed. 
        System.err.println(e); 
       } 
      } 
+0

我知道得到的網址不能爲null excpetion – user3125462