14

I am new to spring. I am developing a CRUD application using spring jdbc template. I am done with insert and select. but in update am facing some problem. can anybody provide me a simple example of update and delete using jdbctemplate. thnks in advance.

MY CONTROLLER-

@RequestMapping(method = RequestMethod.GET)
    public String showUserForm(@ModelAttribute(value="userview")  User user,ModelMap model)
    {
        List list=userService.companylist();
        model.addAttribute("list",list);
        return "viewCompany";
    }

@RequestMapping( method = RequestMethod.POST)
public String add(@ModelAttribute(value="userview") @Valid User user, BindingResult result) 
{
    userValidator.validate(user, result);
    if (result.hasErrors()) {
        return "viewCompany";
    } else {
        userService.updateCompany(user);
        System.out.println("value updated");
        return "updateSuccess";
    }

when i click on update button the edited values should be updated in my DB according to the row ID , my problem is how to map the row id from jsp to controller.

smya.dsh
  • 651
  • 5
  • 10
  • 23
  • What problem do you face? Please post some code and the error you encounter. –  Aug 07 '12 at 10:10
  • scroll to the update methods, many options http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html – NimChimpsky Aug 07 '12 at 10:12
  • there's no error in the console but update operation is not being performed. i shared my controller here.. – smya.dsh Aug 07 '12 at 12:21

2 Answers2

41

Straight from the documentation:

The following example shows a column updated for a certain primary key. In this example, an SQL statement has placeholders for row parameters. The parameter values can be passed in as varargs or alternatively as an array of objects. Thus primitives should be wrapped in the primitive wrapper classes explicitly or using auto-boxing.

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

public class ExecuteAnUpdate {

    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public void setName(int id, String name) {
        this.jdbcTemplate.update(
                "update mytable set name = ? where id = ?", 
                name, id);
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thanks a ton .. i forgot to mention that it should be done in JSP. I cant map JSP value to controller.. – smya.dsh Aug 07 '12 at 10:52
  • 2
    JSPs and controllers have nothing to do with your question. Whether you do it in a JSP or not, the JdbcTemplate API is the same. And no, it should not be done in a JSP. JSPs are used to generate HTML markup. Updating a row in a database has nothing to do in a JSP. – JB Nizet Aug 07 '12 at 11:08
  • the operations which i want to do can be done easily in hibernate. but i dont know hibernate and dont hav time to learn it. so i hav to do it in jdbc template. JSP is mandatory for me because it is my UI – smya.dsh Aug 07 '12 at 11:51
  • The fact that you're using JSPs for your UI shouldn't have any inflence on how you use JdbcTemplate. The API of JdbcTemplate is the same whether you use JSPs or not. And JSPs are used for the **UI**. JdbcTemplate is used to access a database, which has nothing to do with the UI layer, and everything to do with the **data access layer**. You shouldn't have data access code in JSPs. And I have no idea why you bring Hibernate to the table. – JB Nizet Aug 07 '12 at 11:56
  • let's forget about hibernate. my problem is- when i'm clicking the submit button, the update operation should be done in the row selected.how to provide the column id to controller? – smya.dsh Aug 07 '12 at 11:58
  • It all depends on what your HTML code looks like. And that's a completely different question. Ask another question, and show the JSP code in this new question. – JB Nizet Aug 07 '12 at 13:05
  • Interesting that no one mentioned jdbcTemplate.update returns int not void – fiskra Jul 10 '18 at 12:58
  • @fiskra so what? – JB Nizet Jul 10 '18 at 13:03
2

You can simply use request.getParamater() or command object to pass values from jsp to controller.

sonam
  • 875
  • 2
  • 18
  • 37