保存数据到数据库时IP的值为空,syntax(语法)错误解决办法

1 信息: Illegal access: this web application instance has been stopped already.  Could not load .  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
2 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from post set  TITLE='计算机组成原理',sendDate='2017-08-02 10:23:01',ip='' at line 1
3     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
4     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
5     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
6     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
7     at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

以下是后台代码:

 1 @Override
 2     public void updateModel(long postId, IModel model) throws Exception {
 3         //首先判断model是否为空
 4         if(model == null || model.getId() == 0){
 5             throw new NoDataFoundException();//抛出异常
 6         }
 7         //update from post set = 
 8         StringBuffer sql = new StringBuffer();
 9         sql.append("update from "+model.getTableName()+" set ");
10         Map<String,Object> fields = model.getFields();
11         //"keys"是数据库里面表的字段
12         Set<String> keys = fields.keySet();
13         for(Iterator<String> it = keys.iterator();it.hasNext();){
14             String field = it.next();
15             sql.append(field+"=?,");
16         }
17         int sqlLength = sql.length();
18         sql.replace(sqlLength-1, sqlLength, " where id = ?");
19         //给id设值
20         PreparedStatement ps = null;

前台代码:

 1 function complieUpdate(){
 2         //1、首先判断用户是否登录
 3         var isLogin = $('#islogin').val();//islogin是判断用户是否登录的隐藏域的id(title.jsp)
 4         if(isLogin == "false"){
 5             var loginHref = window.location.href;
 6             window.location.href = '<%=ctptitle%>/reg_login/login.jsp?from='+loginHref;
 7             return;
 8         }
 9         var size = '<%=replyList.size()%>';
10         if(size>0 || !${Post.user.id==USER.id}){
11             alert("没有足够的权限!!!");
12         }else{
13             var content = ue1.getContent();
14             $.ajax({
15                 type:"post",
16                 url:"<%=contextPath%>/postServlet?action=update",
17                 data:{postId:'<%=post.getId()%>',content:content},
18                 success:function(data,textStatus){
19                     var result = eval("("+data+")");
20                     if(result.success){
21                         window.location.reload();//强迫浏览器刷新当前页面
22                     }else{
23                         alert(result.errorMsg);
24                     }
25                 },
26                 error:function(XMLHttpRequest,textStatus,errroThrown){
27                     alert("无法连接到服务器");
28                     
29                 }            
30             });
31         }
32         
33     }

遇到问题:首先看下能否把错误信息读懂,不懂得话找下度娘问哈。一般情况下先从前台页面逐一检查,没有问题的话到后台servlet去打个断点调试一遍。

在调试过程中,一般都能够找出问题的原因。语法错误的话,就是代码拼写错误。学习编程最深的痛就是英文没有学好。新人想要学好编程,应该着手从英语开始。

以下是改正后的后台代码:

 1 @Override
 2     public void updateModel(long postId, IModel model) throws Exception {
 3         //首先判断model是否为空
 4         if(model == null || model.getId() == 0){
 5             throw new NoDataFoundException();//抛出异常
 6         }
 7         //update from post set = 
 8         StringBuffer sql = new StringBuffer();
 9         sql.append("update "+model.getTableName()+" set ");
10         Map<String,Object> fields = model.getFields();
11         //"keys"是数据库里面表的字段
12         Set<String> keys = fields.keySet();
13         for(Iterator<String> it = keys.iterator();it.hasNext();){
14             String field = it.next();
15             sql.append(field+"=?,");
16         }
17         int sqlLength = sql.length();
18         sql.replace(sqlLength-1, sqlLength, " where id = ?");

原因是

 sql.append("update "+model.getTableName()+" set ");
update 后面多了from

 

posted on 2017-08-02 10:40  SeaOfDeath  阅读(107)  评论(0)    收藏  举报

导航