2017-11-17 32 views
-2

動作しません:これはFabricanteEditControllerで編集Spring MVCのクラッド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 =? ];エラーデsintaxis:ネストされた例外が発生しましたjava.sql.SQLSyntaxErrorExceptionある「どこで」行1、列の64

根本原因

java.sql.SQLSyntaxErrorException:エラー・ド・sintaxis:「どこで」の行で発生しました1、列64

根本原因

ERROR 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の前の最後の列の後にカンマを付けないでください。

あなたのプログラムを非常に詳しく調べて、updateステートメントでカンマの配置を調べると、問題が見つかると思います。

関連する問題