Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 967
  • Last Modified:

sping jdbctemplate for deleting?

Hi,

I am using the Spring jdbctemplate for my DAO.
I have to delete records from table and this paritcular table has reference integrity on other two tables.
So I have to delete records from other tables before I can delete.

Can I do this in one scoop or do I need three different jdbctemplateS?
Any suggestion?
Thanks.

Delete from Catalog_Price Where cat=?
Delete from Catalog_factory where cat=?
Delete from Catalog where cat=?

Also I have to pass the parameter.
0
dkim18
Asked:
dkim18
4 Solutions
 
dkim18Author Commented:
I will check out the spring doc.
0
 
chaitu chaituCommented:
public void delete(String cat) {
    JdbcTemplate delete = new JdbcTemplate(dataSource);
    delete.update("Delete from Catalog_Price Where cat=?",
        new Object[] { cat });
 delete.update("Delete from Catalog_factory Where cat=?",
        new Object[] { cat });
 delete.update("Delete from Catalog Where cat=?",
        new Object[] { cat });
  }
0
 
Gibu GeorgeCTOCommented:
Another approach is to do the batchUpdate

JdbcTemplate delete = new JdbcTemplate(dataSource);
String[] sqls = new String[]{"Delete from Catalog_Price Where catalog_id=1","Delete from Catalog_factory Where catalog_id=1","Delete from Catalog Where catalog_id=1"};
delete.batchUpdate(sqls);

Open in new window

0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Another possibility, just to put it out there, is that most databases can have the referential integrity setup so that the database cascades the delete to the other tables for you. If this is something that you may be interested in implementing, let us know what DB you are using and we can help set it up.
0
 
Gibu GeorgeCTOCommented:
The cascade delete will work only if while creating referential integrity itself we have to say delete will cascade.

e.g.
ALTER TABLE BookCourses
ADD FOREIGN KEY (Course)
REFERENCES Courses(Code)
ON DELETE CASCADE;

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now