Solved

sping jdbctemplate for deleting?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

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…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

821 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