JdbcTemplate delete syntax
Asked Answered
P

2

12

Can someone point out any mistake in my following code of Spring Jdbc Template?

When I click delete, the record is not getting deleted and there are no errors showing.

public void delete(String id) {
    logger.debug("Deleting existing person");

    // Prepare our SQL statement using Unnamed Parameters style
    String query = "delete from person where id = ?";

    // Assign values to parameters
    Object[] person = new Object[] {id};

    // Delete
    jdbcTemplate.update(query, person);
}
Psephology answered 15/8, 2013 at 19:9 Comment(4)
Do you commit the transaction?Gleanings
I do not understand what do you mean.Please give me example if possible.Is the above code correct?Psephology
Yes your code looks correct. Possibilities are: there is no such person with id "id" / you're connecting to wrong database / you're running this on a unit test framework which rolls back the transaction on completionMargarita
may be you are missing the schema name. Eg : delete from schemaName.tableName where id = ?Orlene
G
26

Here is an example. Pay attention:

Integer id

public boolean delete(Integer id){
    String sql = "DELETE FROM organization WHERE id = ?";
    Object[] args = new Object[] {id};

    return jdbcTemplate.update(sql, args) == 1;
}
Gyron answered 14/7, 2017 at 20:45 Comment(1)
How does this code differ from the original? Explanation of what was wrong would be helpful. The type of the argument is significant, but it's not clear in this answer why getting it wrong results in no error.Hamachi
M
2
@Override
    public String deleteXXById(String id) {
        String sql = "DELETE FROM VENUE WHERE id =:id?";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id", id);

        Object[] args = new Object[] {id};
        int update = jdbcTemplate.update(sql, paramMap);
        String updatecount = "Failed";
        if (update == 0) {
            updatecount = "Failed";
        } else {
            updatecount = "SUCCESS";
        }
         return updatecount;
    }
Messina answered 8/3, 2019 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.