-1

I tried to execute an sql squery in java but they jave been several error of referency constraint. So I have to delete the constraint and add it each time.

I've tried to write an code which do that but it doesn't work :

public void deleteTableDevise() throws SQLException{
              //connexion base
        Statement statement = this.getMyConnection().createStatement();
        //requête suppression
        ArrayList<String> listRequete = new ArrayList<String>();
        String requeteSplit = ""; 

        String deleteDataTable = "ALTER TABLE dbo.T_CORR_STANDARD NOCHECK CONSTRAINT fk_idRIB_t_corr_standard ; truncate table P_DEVISE;";

        requeteSplit= requeteSplit.concat(deleteDataTable);
        if(deleteDataTable.contains(";")){
            listRequete.add(requeteSplit);
            requeteSplit = "";
        }

        for (String uneRequete : listRequete){
            statement.executeUpdate(uneRequete);
        }


        logger.debug("Suppression de la table banque dans la table P_DEVISE - OK");
    }

Thank you.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
harili
  • 406
  • 9
  • 24
  • 1
    Does this answer your question? [How to split a comma-separated string?](https://stackoverflow.com/questions/10631715/how-to-split-a-comma-separated-string) – jhamon Feb 20 '20 at 15:08
  • ther is no split in your code – jhamon Feb 20 '20 at 15:09
  • You don't need to split this. You run both of those statements in a single database call. That is the point of using the statement terminator ; – Sean Lange Feb 20 '20 at 15:16
  • Hello, but split only change a value to another value ? Here, it's like a ';' is converted to ','. My goal is to, when there is a ';', to take the all sentence until the ';' and to execute it. – harili Feb 20 '20 at 15:19
  • In the example above your string has two statements. Are you not going to run both of them? Assuming that is the case you don't need to split them and them individually. – Sean Lange Feb 20 '20 at 15:41
  • Yes yes, I'm going to run both of them but individually otherwise it won't work. But it can work if I declare two variables ? – harili Feb 20 '20 at 15:48
  • @SeanLange The JDBC API is designed with the expectation of executing a single statement per execute. – Mark Rotteveel Feb 20 '20 at 16:01
  • @harili you should really read the String.split documentation. Or the link I posted – jhamon Feb 20 '20 at 16:03
  • @MarkRotteveel that's odd...but whatever. – Sean Lange Feb 20 '20 at 16:20
  • Since you just have two hard coded statements that you need to execute why not just split this apart in your code instead of string splitting and looping? Seems like it would be a lot simpler. – Sean Lange Feb 20 '20 at 16:20
  • @jhamon Hello, so yes I've read the .split documentation and I understood the point of this option. So I could put my queries into a list and read it in a loop. At each turn, I execute my sql query – harili Feb 21 '20 at 08:22

1 Answers1

0

So It could be somethings like this :

public void deleteTableDevise() throws SQLException{
              //connexion base
        Statement statement = this.getMyConnection().createStatement();
        //requête suppression


        String deleteDataTable = "ALTER TABLE dbo.T_CORR_STANDARD NOCHECK CONSTRAINT fk_idRIB_t_corr_standard ; truncate table P_DEVISE;";
        ArrayList<String> listRequete = new ArrayList<String>();
        listRequete = deleteDataTable.split(";")

        for (String uneRequete : listRequete){
            statement.executeUpdate(uneRequete);
        }


        logger.debug("Suppression de la table banque dans la table P_DEVISE - OK");
    }

Didn't hesitate to give me ideas for improvement ! Thank you for the help.

harili
  • 406
  • 9
  • 24