Solved

sping jdbctemplate for deleting?

Posted on 2014-03-31
5
481 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bitbucket vs gitbucket 3 57
Groovy:unable to resolve class error 2 66
oracle 11g 23 47
reverse digits of a number using for loop 5 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

21 Experts available now in Live!

Get 1:1 Help Now