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)
Unknown column 'assd' in 'field list'。看一下<input type="text" name="username" size="15">这个这么赋值给User对象的属性的?
自己解决了。是因为SQL语句写的不对。。应该这样写:
String sql="update user set name='"+user.getName()+"',password='"+user.getPassword()+"',sex='"+user.getAge()+"',age='"+user.getAge()+"' where id='"+user.getId()+"'";
这样就OK了