2017-11-17 178 views
-2

这是FabricanteEditController:不起作用编辑Spring MVC的CRUD的JdbcTemplate

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服务器错误

descriptionThe服务器遇到内部错误,阻止它履行这一要求。

例外

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

根源

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

根源

java.sql.SQLSyntaxErrorException:错误德sintaxis:遇到“其中”在线1,列64

根源

错误42X01:错误德sintaxis:遇到 “其中” 1行,列64

+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之前的最后一列之后没有逗号。

我相信如果你仔细检查你的程序,看看你的语句中逗号的位置,你会发现这个问题。