数据库中出现奇怪的事
我出现一个问题,就是我原来的密码是123456,如果要修改555aaa,方法如下
:
//更改用户的密码
public boolean updatePaw(String name,String pw2){
boolean result = false;
Connection conn = null;
conn = this.getConnection();
String sql ="update mailUser set passWord ="+pw2+" where loginName = '"+name+"'";
try
{
PreparedStatement ps = conn.prepareStatement(sql);
int rs = ps.executeUpdate();
if(rs ==1){
result = true;
}else {
result = false;
}
ps.close();
conn.close();
} catch (
SQLException e)
{
e.printStackTrace();
}
return result;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
ChangeDao chDao = new ChangeDao();
chDao.updatePaw("pengzhongyan", "12345y"); //改成“123fhf”,就会包如下的错误
}进行单元测试出现错误:
java.sql.SQLException: 'y' 附近有语法错误。
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
------解决方案--------------------
这句SQL对字符串漏了单引号:
String sql ="update mailUser set passWord ="+pw2+" where loginName = '"+name+"'";
应修改为:
String sql ="update mailUser set passWord ='"+pw2+"' where loginName = '"+name+"'";