Advertisement

03.06.2007 at 01:50AM PST, ID: 22430510
[x]
Attachment Details

Java SQL delete records with children

Asked by kristian_gr in PostgreSQL Database, Java Programming Language, SQL Query Syntax

Tags: delete, sql, java, records

Hi.
I’m trying to create a java class that deletes all data in a db. When it’s done I’d like only empty tables. The problem is that I don’t know anything about the tables and the structure.

I’ve got two methods for this, the firs one, getTabs(), returns all tables in my db. The second one does DELETE FROM <table>. This works as long as there is no connection to other tables.
But if one of the records is used in another table as FOREIGN KEY,  I get an : “violates foreign key constraint” message.

This can be fixed doing “DELETE FROM <table>” in a correct order. But I need to figure out a way to “find” the correct order.

Any one who has any ideas?

[code]

public List<String> getTabs() {
        List<String> tabeller = new ArrayList<String>();
        try {
            Statement st = conn.createStatement();
            DatabaseMetaData dmd = conn.getMetaData();
            String types[] = {"TABLE"};
            ResultSet rs = dmd.getTables(null, null, null, types);
            while(rs.next()) {
                tabeller.add(rs.getString("TABLE_NAME"));
               
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return tabeller;
    }

public void emptyTabs(List<String> tabs) {
       Statement st;
       try {
           st = conn.createStatement();
           for(String a : tabs) {
               st.execute("DELETE FROM " + a + ";");
           }
           st.close();
       }catch (SQLException ex) {
           ex.printStackTrace();
       }
    }
[/code]
Start Free Trial
[+][-]03.06.2007 at 02:25AM PST, ID: 18660511

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2007 at 03:00AM PST, ID: 18660629

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2007 at 03:47AM PST, ID: 18660809

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2007 at 04:29AM PST, ID: 18660973

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2007 at 04:29AM PST, ID: 18660977

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2007 at 07:12AM PST, ID: 18662050

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2007 at 10:52AM PST, ID: 18664263

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PostgreSQL Database, Java Programming Language, SQL Query Syntax
Tags: delete, sql, java, records
Sign Up Now!
Solution Provided By: mayankeagle
Participating Experts: 6
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32