Solved

sping jdbctemplate for deleting?

Posted on 2014-03-31
5
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 36

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

626 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