当前位置: 代码迷 >> Eclipse >> 为啥到executeUpdate(sql)这一句,就不执行了呢?求指教解决方法
  详细解决方案

为啥到executeUpdate(sql)这一句,就不执行了呢?求指教解决方法

热度:100   发布时间:2016-04-23 14:04:21.0
为啥到executeUpdate(sql)这一句,就不执行了呢?求指教
我的代码
String sql = "UPDATE ACCOUNTS SET 密码=" + account.getPassword()
+ " WHERE 账户名=" + account.getAccountName();

try {
Class.forName(this.dbDriver).newInstance();
} catch (Exception e) {
e.printStackTrace();
}

try {
Connection conn = DriverManager.getConnection(this.dbUrl,
this.user, this.password);
if (!conn.isClosed()) {
Statement st = conn.createStatement();
Account acc = this.getAccountInfo(account.getAccountName());
if (acc != null) {
System.out.println(2);
int count = st.executeUpdate(sql);
System.out
.println(count
+ " account's information has been updated in table ACCOUNTS!");
} else
System.out
.println("There isn't the account which you want to update its information!");
}
} catch (SQLException e) {

}

------解决方案--------------------
sql 语句不对,
String sql = "UPDATE ACCOUNTS SET 密码='" + account.getPassword()
+ "' WHERE 账户名='" + account.getAccountName()+"'";

最好是用PreparedStatement 
String sql = "UPDATE ACCOUNTS SET 密码=? WHERE 账户名=?";
PreparedStatement pstmt = conn.createPreparedStatement(sql);
pstmt.setString(1,account.getPassword());
pstmt.setString(2,account.getAccountName());int rs = pstmt.executeUpdate();
  相关解决方案