Solved

sping jdbctemplate for deleting?

Posted on 2014-03-31
5
452 Views
Last Modified: 2014-04-02
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
Comment
Question by:dkim18
5 Comments
 

Author Comment

by:dkim18
ID: 39967257
I will check out the spring doc.
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 200 total points
ID: 39968581
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
 
LVL 12

Assisted Solution

by:Gibu George
Gibu George earned 250 total points
ID: 39968819
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
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 50 total points
ID: 39969007
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
 
LVL 12

Assisted Solution

by:Gibu George
Gibu George earned 250 total points
ID: 39969196
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now