![](/img/trans.png)
[英]I'm making a payroll using Java. Why am I getting the wrong net pay if my mathematical formulas seem correct?
[英]I'm trying to update my password in oracle database using Java. Am I doing something wrong?
即使單擊按鈕更改密碼,運行模擬器時也不會出現任何錯誤。 我已經嘗試了所有可以想到的方法,有人知道我是否做錯了什么。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_account);
更改密碼的按鈕
Button password = (Button) findViewById(R.id.btnChangePassword);
password.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
update();
} catch (Exception e) {
e.printStackTrace();
}
}
});}
密碼更新方法
public Exception update()throws Exception{
Connection conn = null;
Statement st = null;
try {
conn = ConnectionManager.getConnection();
st = conn.createStatement();
String select_password = "SELECT * From Userinfo Where USERID = 5";
ResultSet Password = st.executeQuery(select_password);
String password = "UPDATE userinfo SET PASSWORD = ? WHERE USERID = 5";
while (Password.next()) {
PreparedStatement ps = null; //conn.prepareStatement(Update_Bank);
ps = conn.prepareStatement(password);
String pass = "password123";
ps.setString(1, pass);
ps.executeUpdate();
ps.close();
}
}catch(Exception e){
return e;
}
return null;
}}
好的,所以我做了一些修改,遍歷了代碼,發現它在“ ResultSet Password = st.executeQuery(select_password);”處失敗。 聲明。 問題是它可以在Eclipse中工作,但不能在android studio中工作。 有人對為什么有任何想法嗎?
Connection conn = null;
Statement st = null;
conn = ConnectionManager.getConnection();
st = conn.createStatement();
String select_password = "SELECT * From Userinfo Where USERID = 5";
ResultSet Password = st.executeQuery(select_password);
String password =( "UPDATE userinfo SET PASSWORD = ? WHERE USERID = 5");
while (Password.next()) {
PreparedStatement ps = conn.prepareStatement(password);
ps = conn.prepareStatement(password);
String pass = "password";
ps.setString(1, pass);
ps.executeUpdate();
ps.close();
}
}
您不會有任何異常,因為您在update內有一個catch all語句,通過忽略update的返回值可以完全忽略它!
修復后,請執行以下操作:
確保while循環內的代碼正在執行
檢查executeUpdate的返回值是否大於0
如果不熟悉用於更改密碼的SQL語法,您是否嘗試過直接在控制台中運行它?
和一些建議:
您不需要查詢,因為您沒有使用它的結果集。
切勿使用ID。 請改用用戶名。
關閉連接
切勿捕獲所有異常。 如果這樣做,則永遠不要忽略何時引發異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.