首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

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

2011-12-30 
为啥到executeUpdate(sql)这一句,就不执行了呢?求指教我的代码String sql UPDATE ACCOUNTS SET 密码

为啥到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();

热点排行