不工作编辑spring mvc crud jdbctemplate

时间:2017-11-17 23:40:13

标签: spring-mvc crud derby jdbctemplate

这是FabricanteEditController:

package Controller;

import Modelo.Conectar;
import Modelo.Fabricante;
import Modelo.FabricanteValidar;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
*
* @author x2010s
*/

@Controller
@RequestMapping("fabricanteedit.htm")
public class FabricanteEditController {

FabricanteValidar fabricanteValidar;
private JdbcTemplate jdbcTemplate;

public FabricanteEditController() 
{
   this.fabricanteValidar=new FabricanteValidar();
   Conectar con=new Conectar();
   this.jdbcTemplate=new JdbcTemplate(con.conectar() );
}

@RequestMapping(method=RequestMethod.GET) 
public ModelAndView form(HttpServletRequest request)
{
    ModelAndView mav=new ModelAndView();
    int id=Integer.parseInt(request.getParameter("id"));
    Fabricante datos=this.selectFabricante(id);
    mav.setViewName("fabricanteedit");
    mav.addObject("fabricante",new  Fabricante(id,datos.getNombre(),datos.getDireccion(),datos.getTelefono(),datos.getTipo()));
    return mav;
}

 @RequestMapping(method=RequestMethod.POST)
 public ModelAndView form
    (
            @ModelAttribute("fabricante") Fabricante f,
            BindingResult result,
            SessionStatus status,
            HttpServletRequest request
    )
{
 this.fabricanteValidar.validate(f, result);
    if(result.hasErrors())
    {
         ModelAndView mav=new ModelAndView();
        int id=Integer.parseInt(request.getParameter("id"));
        Fabricante datos=this.selectFabricante(id);
        mav.setViewName("fabricanteedit");
        mav.addObject("fabricante",new Fabricante(id,datos.getNombre(),datos.getDireccion(),datos.getTelefono(),datos.getTipo()));
        return mav;
    }else
    {
        int id=Integer.parseInt(request.getParameter("id"));
    this.jdbcTemplate.update(
                "update fabricante "
            + "set nombre=?,"
            + "direccion=?,"
            + "telefono=? "
            + "tipo=? "
            + "where "
            + "id=? ",
     f.getNombre(),f.getDireccion(),f.getTelefono(),f.getTipo(),id);
     return new ModelAndView("redirect:/fabricante.htm");
    }   

}
    public Fabricante selectFabricante(int id) 
{
    final Fabricante maker = new Fabricante();
    String quer = "SELECT * FROM fabricante WHERE id="+id;
    return (Fabricante) jdbcTemplate.query
    (
            quer, new ResultSetExtractor<Fabricante>() 
        {
            public Fabricante extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (rs.next()) {
                    maker.setNombre(rs.getString("nombre"));
                    maker.setDireccion(rs.getString("direccion"));
                    maker.setTelefono(rs.getString("telefono"));
                    maker.setTipo(rs.getString("tipo"));

                }
                return maker;
            }


        }
    );
    }
}

这是视图fabricanteedit.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"   %>
<%@taglib  prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Editar Fabricante</title>
    <link href="css/estilos.css" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet" >
</head>
<body>
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="<c:url value="/fabricante.htm" />">Listado de  fabricante</a></li>
            <li class="active">Editar</li>
        </ol>
        <div class="panel panel-primary">
            <div class="panel-heading">Editar</div>
            <div class="panel-body">

                    <form:form method="post" commandName="fabricante">
                        <h1>Complete el formulario</h1>

                        <form:errors path="*" element="div"  cssClass="alert alert-danger" />

                       <p>
                            <form:label path="nombre">Nombre:  </form:label>
                            <form:input path="nombre" cssClass="form-control" />

                        </p>

                        <p>
                            <form:label  path="direccion">Direccion</form:label>
                            <form:input path="direccion" cssClass="form-control" />
                        </p>

                        <p>
                            <form:label path="telefono">Telefono</form:label>
                            <form:input path="telefono" cssClass="form-control" />
                        </p>

                        <p>
                            <form:label path="tipo">Tipo</form:label>
                            <form:input path="tipo" cssClass="form-control" />
                        </p>

                        <hr />
                        <input type="submit" value="Enviar" class="btn btn-danger" />
                    </form:form>
              </div>
          </div>
      </div>
  </body>

我的数据库是德比。

尝试编辑制造商时,会发生以下错误:

HTTP状态500 - 内部服务器错误

输入例外报告

messageInternal Server Error

description服务器遇到内部错误,导致无法完成此请求。

例外

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback;错误的SQL语法[更新fabricante set nombre = ?, direccion = ?, telefono = ?, tipo = ?,其中id =? ]。嵌套异常是java.sql.SQLSyntaxErrorException:错误de sintaxis:在第1行第64列遇到“where”。

根本原因

org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback;错误的SQL语法[更新fabricante set nombre = ?, direccion = ?, telefono = ?, tipo = ?,其中id =? ]。嵌套异常是java.sql.SQLSyntaxErrorException:错误de sintaxis:在第1行第64列遇到“where”。

根本原因

java.sql.SQLSyntaxErrorException:Error de sintaxis:在第1行第64列遇到“where”。

根本原因

ERROR 42X01:错误de sintaxis:在第1行第64列遇到“where”。

1 个答案:

答案 0 :(得分:0)

在您的问题代码中,您显示:

    this.jdbcTemplate.update(
            "update fabricante "
        + "set nombre=?,"
        + "direccion=?,"
        + "telefono=? "
        + "tipo=? "
        + "where "
        + "id=? ",

在您在问题中引用的错误消息中,显示:

bad SQL grammar [update fabricante set nombre=?,direccion=?,telefono=?, tipo=?, where id=? ];

这两个看起来都不对。

在您显示的代码中,您在"telefono=? "之后缺少逗号。

在您显示的错误消息中,tipo=?,后面有额外逗号

令人困惑的是,这两件事并不匹配。

但我相信两者都不正确。您应该在每个column=?表达式之间使用逗号,而在where之前的最后一列之后不能使用逗号。

我相信,如果您仔细检查自己的计划,在update声明中查看逗号的位置,就会发现问题。