2012-08-27 15 views
1

我正在研究使用Java Spring和JDBC的CRUD应用程序。我看过教程,但我卡住了“U”字母:)。 我做了一些事情,但如果你能帮助我,我将不胜感激。java CRUD应用程序卡在“更新”上!

这里有事情,我做:

TournamentDAO:

public void edit(Tournament tournament){ 
    String sql = "update TOURNAMENT.TOURNAMENT set name = ? , location = ? , date = ? , where id = ?"; 

    m_jdbcTemplate.update(sql, 
       new Object[]{ 
       tournament.getName(), 
       tournament.getLocation(), 
       tournament.getDate(), 
       Long.toString(tournament.getId()) 
    }); 
} 

控制器 “EditTournament”:

@Controller 
public class EditTournament { 

    @Autowired 
    private TournamentService turnirSer; 

    @RequestMapping(value = "/editTournament", method = RequestMethod.GET) 
    public ModelAndView edit(@RequestParam("id") Integer id) { 
     ModelAndView mav = new ModelAndView("editTournament"); 
     Tournament Tournament = turnirSer.getById(id); 
     mav.addObject("editTournament", tournament); 
     return mav; 
    } 

    @RequestMapping(value = "/editTournament", method = RequestMethod.POST) 
    public String update(Model model, @Valid Tournament tournament, 
      BindingResult bindingResult, 
      @ModelAttribute("tournamentList") ArrayList<Tournament> TournamentList) { 
     if (!bindingResult.hasErrors()) { 
      turnirSer.editTournament(tournament); 
      tournamentList.add(tournament);   
     } 
     return "editTournament"; 
    } 

} 

现在只是说我也知道这是完全是一个烂摊子这为什么我需要你的帮助!

在这一行:

Tournament Tournament = turnirSer.getById(id); 

我没办法getById(id),我把它形成基于Hibernate的,我不使用教程,我不知道如何创建一个!

有人可以请解释我什么是错的,以及如何编写此代码,以便我的更新工作。

PS。创建,读取和删除工作正常。在这里发布这个问题是我的最后一个选择,我已经做了我能做的。

+0

最好是显示异常的堆栈跟踪。 – MJM

回答

4

你有多余的

, 

查询where

date = ? , where id = ?"; 
0

您可以在TournamentDAO(或它的超类,如果你想实现它咬了通用)与又多了一个方法名称getById(String id),执行情况类似于

public Tournament getById(String id){ 
    String sql = "select name, location, date from TOURNAMENT.TOURNAMENT where id = :id"; 
    List<Map<String, String>> results = m_jdbcTemplate.queryForList(sql, new HashMap<String, String>(){{put("id", id)}}); 
    //as id would be the primary key, at max you may one record (one entry in the list). 
    Map<String, Object> result = results.get(0); //add a check for no results 
     Tournament t = new Tournament(); 
     t.setName(result.get("name")); 
     //set all your properties as mentioned above 
    return t; 
    } 

jdbctemplate还提供其他选项,如RowMaperRowCallbackHandler,这将帮助您自动化硬编码属性名称和所有属性。

有关基本示例,请参阅this link