java
  • servlets
  • 2017-05-26 48 views 1 likes 
    1

    我在做更改密碼我需要更新舊密碼。基於舊密碼,我需要得到記錄,然後更新記錄,但這裏是我試圖更新它時,我得到了創紀錄的問題顯示在消息空無法在java servlet中執行更新查詢

    我的代碼:

    public String ResetPwd(String NewPwd, String name,String oldpwd) 
        { 
         String Pass="Select * from Users where Password='"+oldpwd+"' and UserId='"+name+"'"; 
         String UpdateQuery="update Users set Password='"+NewPwd+"' where UserId='"+name+"'"; 
         try{ 
          currentCon = ConnectionManager.getConnection(); 
          st=currentCon.createStatement();   
          rs = st.executeQuery(Pass); 
          if(rs.next()) 
          {   
           PreparedStatement ps=currentCon.prepareStatement(UpdateQuery);     
           int i=ps.executeUpdate(); 
           ps.close();   
    
           if(i>=1) 
           { 
            msg="Password Changed Successfully"; 
           } 
          } 
          else{ 
           msg="Old Password Not Match Please Enter Correct Password..!"; 
           return msg; 
          } 
          }catch(Exception ex){} 
          return msg; 
        } 
    
    +3

    你剛剛刪除了你的最後一個問題然後轉貼......爲什麼?哦,你還沒有在'catch'塊中添加'e.printStackTrace()'....可能想這麼做,有很多重要信息可以從簡單的堆棧跟蹤中獲得js – CraigR8806

    +1

    爲什麼要執行2個executeUpdate命令?第二個是在ps.close()之後啓動的,這是行不通的。 – Galcoholic

    回答

    1

    msg爲null,因爲可能一些異常正在拋出,它不會被設置爲任何東西。正如@ CraigR8806所說的,不要忽略你捕獲的例外,但至少要打印它們。

    所引發的異常可能是SQLException,因爲你正在呼籲 的executeUpdate對已經在這一點上

      PreparedStatement ps=currentCon.prepareStatement(UpdateQuery); 
          ps.executeUpdate(); 
          ps.close(); 
          int i=ps.executeUpdate(); 
          ps.close(); 
    

    關閉preparedStatament因爲沒有理由爲它第二次更新變化:

    PreparedStatement ps=currentCon.prepareStatement(UpdateQuery); 
        int i=ps.executeUpdate(); 
        ps.close(); 
    

    作爲一個便箋使用嘗試資源,因爲它有助於關閉這些資源

    +0

    嗨,@Zeromus,我試着用你的代碼也仍然是我得到相同的味精是空 –

    +1

    正如我所說︰改變} catch(Exception ex){} to} catch(Exception ex){System.out.println(ex) ;}至少知道發生了什麼。 然後編輯您的問題與錯誤消息 – Zeromus

    +0

    謝謝你的想法給我的想法我是這個Java平臺的更新我麻獲得錯誤隱式轉換從數據類型varchar到varbinary是不允許的。使用CONVERT函數來運行這個查詢。在我的數據庫中,密碼數據類型是varbinary數據 –

    相關問題