首页 新闻 会员 周边 捐助

MySQL更新语句出现问题:为什么当<input type="text" name="username" size="15"> 中输入数字能更新成功

1
悬赏园豆:5 [已关闭问题] 关闭于 2011-12-14 12:41
public void updateUser (User user) { 
conn=ju.getConn();
PreparedStatement stmt=null;
try {

String sql="update user set name="+user.getName()+",password="+user.getPassword()+",sex="+user.getAge()+",age="+user.getAge()+" where id="+user.getId();
stmt=conn.prepareStatement(sql);
stmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



<input type="text" name="username" size="15"> 输入字母:assd 出现如下问题:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'assd' in 'field list'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at com.personinformation.dao.UserDAO.updateUser(UserDAO.java:61)
at com.personinformation.web.ModifyServlet.doPost(ModifyServlet.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
柠檬Cool的主页 柠檬Cool | 初学一级 | 园豆:195
提问于:2011-12-14 10:26
< >
分享
所有回答(2)
-1

 Unknown column 'assd' in 'field list'。看一下<input type="text" name="username" size="15">这个这么赋值给User对象的属性的?

悟行 | 园豆:12559 (专家六级) | 2011-12-14 10:30
0
自己解决了。是因为SQL语句写的不对。。应该这样写:
String sql="update user set name='"+user.getName()+"',password='"+user.getPassword()+"',sex='"+user.getAge()+"',age='"+user.getAge()+"' where id='"+user.getId()+"'";

这样就OK了

柠檬Cool | 园豆:195 (初学一级) | 2011-12-14 12:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册