How to check if a database is empty using java












-1














I have some functionality which needs the database to be clean, How do i check if my database is clean using JAVA ?
I have already written the code about whether the Database exists or not but I couldn't find any info on how to check if it's clean.










share|improve this question


















  • 1




    count the rows in every table? Or just clear the tables in the first place, instead of expecting something else to do it beforehand.
    – JB Nizet
    Nov 23 '18 at 9:54






  • 1




    depends on the database that you use, there are queries that you can get all table names. then iterate that result and execute select (only get the rowcount) for each and every table.
    – hunter
    Nov 23 '18 at 9:57










  • that is one way to do it , but my DB has over 100 tables so i am not sure if it is the right way to do it ? also I should thrown an exception if the DB is not empty so cleaning the table is not an option
    – RajieRoo
    Nov 23 '18 at 9:57










  • Your request that the database must be empty is a little weird but this is something you need to solve yourself since there is no general solution for it. Maybe you don't need to check all table but only a few key ones?
    – Joakim Danielson
    Nov 23 '18 at 10:01






  • 1




    @RajieRoo There's no other way... In any case even for 100 tables it should be fast with count().
    – m0skit0
    Nov 23 '18 at 10:01


















-1














I have some functionality which needs the database to be clean, How do i check if my database is clean using JAVA ?
I have already written the code about whether the Database exists or not but I couldn't find any info on how to check if it's clean.










share|improve this question


















  • 1




    count the rows in every table? Or just clear the tables in the first place, instead of expecting something else to do it beforehand.
    – JB Nizet
    Nov 23 '18 at 9:54






  • 1




    depends on the database that you use, there are queries that you can get all table names. then iterate that result and execute select (only get the rowcount) for each and every table.
    – hunter
    Nov 23 '18 at 9:57










  • that is one way to do it , but my DB has over 100 tables so i am not sure if it is the right way to do it ? also I should thrown an exception if the DB is not empty so cleaning the table is not an option
    – RajieRoo
    Nov 23 '18 at 9:57










  • Your request that the database must be empty is a little weird but this is something you need to solve yourself since there is no general solution for it. Maybe you don't need to check all table but only a few key ones?
    – Joakim Danielson
    Nov 23 '18 at 10:01






  • 1




    @RajieRoo There's no other way... In any case even for 100 tables it should be fast with count().
    – m0skit0
    Nov 23 '18 at 10:01
















-1












-1








-1







I have some functionality which needs the database to be clean, How do i check if my database is clean using JAVA ?
I have already written the code about whether the Database exists or not but I couldn't find any info on how to check if it's clean.










share|improve this question













I have some functionality which needs the database to be clean, How do i check if my database is clean using JAVA ?
I have already written the code about whether the Database exists or not but I couldn't find any info on how to check if it's clean.







java database






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 9:52









RajieRooRajieRoo

106




106








  • 1




    count the rows in every table? Or just clear the tables in the first place, instead of expecting something else to do it beforehand.
    – JB Nizet
    Nov 23 '18 at 9:54






  • 1




    depends on the database that you use, there are queries that you can get all table names. then iterate that result and execute select (only get the rowcount) for each and every table.
    – hunter
    Nov 23 '18 at 9:57










  • that is one way to do it , but my DB has over 100 tables so i am not sure if it is the right way to do it ? also I should thrown an exception if the DB is not empty so cleaning the table is not an option
    – RajieRoo
    Nov 23 '18 at 9:57










  • Your request that the database must be empty is a little weird but this is something you need to solve yourself since there is no general solution for it. Maybe you don't need to check all table but only a few key ones?
    – Joakim Danielson
    Nov 23 '18 at 10:01






  • 1




    @RajieRoo There's no other way... In any case even for 100 tables it should be fast with count().
    – m0skit0
    Nov 23 '18 at 10:01
















  • 1




    count the rows in every table? Or just clear the tables in the first place, instead of expecting something else to do it beforehand.
    – JB Nizet
    Nov 23 '18 at 9:54






  • 1




    depends on the database that you use, there are queries that you can get all table names. then iterate that result and execute select (only get the rowcount) for each and every table.
    – hunter
    Nov 23 '18 at 9:57










  • that is one way to do it , but my DB has over 100 tables so i am not sure if it is the right way to do it ? also I should thrown an exception if the DB is not empty so cleaning the table is not an option
    – RajieRoo
    Nov 23 '18 at 9:57










  • Your request that the database must be empty is a little weird but this is something you need to solve yourself since there is no general solution for it. Maybe you don't need to check all table but only a few key ones?
    – Joakim Danielson
    Nov 23 '18 at 10:01






  • 1




    @RajieRoo There's no other way... In any case even for 100 tables it should be fast with count().
    – m0skit0
    Nov 23 '18 at 10:01










1




1




count the rows in every table? Or just clear the tables in the first place, instead of expecting something else to do it beforehand.
– JB Nizet
Nov 23 '18 at 9:54




count the rows in every table? Or just clear the tables in the first place, instead of expecting something else to do it beforehand.
– JB Nizet
Nov 23 '18 at 9:54




1




1




depends on the database that you use, there are queries that you can get all table names. then iterate that result and execute select (only get the rowcount) for each and every table.
– hunter
Nov 23 '18 at 9:57




depends on the database that you use, there are queries that you can get all table names. then iterate that result and execute select (only get the rowcount) for each and every table.
– hunter
Nov 23 '18 at 9:57












that is one way to do it , but my DB has over 100 tables so i am not sure if it is the right way to do it ? also I should thrown an exception if the DB is not empty so cleaning the table is not an option
– RajieRoo
Nov 23 '18 at 9:57




that is one way to do it , but my DB has over 100 tables so i am not sure if it is the right way to do it ? also I should thrown an exception if the DB is not empty so cleaning the table is not an option
– RajieRoo
Nov 23 '18 at 9:57












Your request that the database must be empty is a little weird but this is something you need to solve yourself since there is no general solution for it. Maybe you don't need to check all table but only a few key ones?
– Joakim Danielson
Nov 23 '18 at 10:01




Your request that the database must be empty is a little weird but this is something you need to solve yourself since there is no general solution for it. Maybe you don't need to check all table but only a few key ones?
– Joakim Danielson
Nov 23 '18 at 10:01




1




1




@RajieRoo There's no other way... In any case even for 100 tables it should be fast with count().
– m0skit0
Nov 23 '18 at 10:01






@RajieRoo There's no other way... In any case even for 100 tables it should be fast with count().
– m0skit0
Nov 23 '18 at 10:01














1 Answer
1






active

oldest

votes


















-1














take a look at that code:



public static void createLocalDB(String driver, String url, String user, String password) {
Connection connection = null;
Statement statement;

try {
connection = SQLUtils.getDBConnection(driver,url,user,password);
connection.setAutoCommit(true);
statement = connection.createStatement();
statement.execute("CREATE SEQUENCE IF NOT EXISTS INTERACTION_SEQ;");
statement.execute("CREATE TABLE IF NOT EXISTS interaction(id CHAR(18) PRIMARY KEY,sequence BIGINT DEFAULT INTERACTION_SEQ.NEXTVAL,type CHAR(20),startdate TIMESTAMP,enddate TIMESTAMP,timeshift INT DEFAULT 0,xml CLOB, finished BOOLEAN DEFAULT false);");
statement.execute("CREATE INDEX IF NOT EXISTS date_index_interaction ON interaction(startdate);");
statement.execute("CREATE INDEX IF NOT EXISTS seq_index_interaction ON interaction(sequence);");
} catch (SQLException | ClassNotFoundException e) {
log.error("Error creating new local db, message:{}",e.getMessage());
} finally {
if( connection != null ) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
}


this method can be used by you in your project, the aim of the method is to create the database if that is empty (do not exist). Just adapt the queries for your database tables, and it will be able to generate sequences, tables, etc. Notice that my SQL syntax is for H2 database, then you need to adapt to your own.



If you have any further question just let me know, I hope to help you out with that,



Cheers,



-Rod






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444287%2fhow-to-check-if-a-database-is-empty-using-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    take a look at that code:



    public static void createLocalDB(String driver, String url, String user, String password) {
    Connection connection = null;
    Statement statement;

    try {
    connection = SQLUtils.getDBConnection(driver,url,user,password);
    connection.setAutoCommit(true);
    statement = connection.createStatement();
    statement.execute("CREATE SEQUENCE IF NOT EXISTS INTERACTION_SEQ;");
    statement.execute("CREATE TABLE IF NOT EXISTS interaction(id CHAR(18) PRIMARY KEY,sequence BIGINT DEFAULT INTERACTION_SEQ.NEXTVAL,type CHAR(20),startdate TIMESTAMP,enddate TIMESTAMP,timeshift INT DEFAULT 0,xml CLOB, finished BOOLEAN DEFAULT false);");
    statement.execute("CREATE INDEX IF NOT EXISTS date_index_interaction ON interaction(startdate);");
    statement.execute("CREATE INDEX IF NOT EXISTS seq_index_interaction ON interaction(sequence);");
    } catch (SQLException | ClassNotFoundException e) {
    log.error("Error creating new local db, message:{}",e.getMessage());
    } finally {
    if( connection != null ) {
    try {
    connection.close();
    } catch (SQLException e) {
    }
    }
    }
    }


    this method can be used by you in your project, the aim of the method is to create the database if that is empty (do not exist). Just adapt the queries for your database tables, and it will be able to generate sequences, tables, etc. Notice that my SQL syntax is for H2 database, then you need to adapt to your own.



    If you have any further question just let me know, I hope to help you out with that,



    Cheers,



    -Rod






    share|improve this answer


























      -1














      take a look at that code:



      public static void createLocalDB(String driver, String url, String user, String password) {
      Connection connection = null;
      Statement statement;

      try {
      connection = SQLUtils.getDBConnection(driver,url,user,password);
      connection.setAutoCommit(true);
      statement = connection.createStatement();
      statement.execute("CREATE SEQUENCE IF NOT EXISTS INTERACTION_SEQ;");
      statement.execute("CREATE TABLE IF NOT EXISTS interaction(id CHAR(18) PRIMARY KEY,sequence BIGINT DEFAULT INTERACTION_SEQ.NEXTVAL,type CHAR(20),startdate TIMESTAMP,enddate TIMESTAMP,timeshift INT DEFAULT 0,xml CLOB, finished BOOLEAN DEFAULT false);");
      statement.execute("CREATE INDEX IF NOT EXISTS date_index_interaction ON interaction(startdate);");
      statement.execute("CREATE INDEX IF NOT EXISTS seq_index_interaction ON interaction(sequence);");
      } catch (SQLException | ClassNotFoundException e) {
      log.error("Error creating new local db, message:{}",e.getMessage());
      } finally {
      if( connection != null ) {
      try {
      connection.close();
      } catch (SQLException e) {
      }
      }
      }
      }


      this method can be used by you in your project, the aim of the method is to create the database if that is empty (do not exist). Just adapt the queries for your database tables, and it will be able to generate sequences, tables, etc. Notice that my SQL syntax is for H2 database, then you need to adapt to your own.



      If you have any further question just let me know, I hope to help you out with that,



      Cheers,



      -Rod






      share|improve this answer
























        -1












        -1








        -1






        take a look at that code:



        public static void createLocalDB(String driver, String url, String user, String password) {
        Connection connection = null;
        Statement statement;

        try {
        connection = SQLUtils.getDBConnection(driver,url,user,password);
        connection.setAutoCommit(true);
        statement = connection.createStatement();
        statement.execute("CREATE SEQUENCE IF NOT EXISTS INTERACTION_SEQ;");
        statement.execute("CREATE TABLE IF NOT EXISTS interaction(id CHAR(18) PRIMARY KEY,sequence BIGINT DEFAULT INTERACTION_SEQ.NEXTVAL,type CHAR(20),startdate TIMESTAMP,enddate TIMESTAMP,timeshift INT DEFAULT 0,xml CLOB, finished BOOLEAN DEFAULT false);");
        statement.execute("CREATE INDEX IF NOT EXISTS date_index_interaction ON interaction(startdate);");
        statement.execute("CREATE INDEX IF NOT EXISTS seq_index_interaction ON interaction(sequence);");
        } catch (SQLException | ClassNotFoundException e) {
        log.error("Error creating new local db, message:{}",e.getMessage());
        } finally {
        if( connection != null ) {
        try {
        connection.close();
        } catch (SQLException e) {
        }
        }
        }
        }


        this method can be used by you in your project, the aim of the method is to create the database if that is empty (do not exist). Just adapt the queries for your database tables, and it will be able to generate sequences, tables, etc. Notice that my SQL syntax is for H2 database, then you need to adapt to your own.



        If you have any further question just let me know, I hope to help you out with that,



        Cheers,



        -Rod






        share|improve this answer












        take a look at that code:



        public static void createLocalDB(String driver, String url, String user, String password) {
        Connection connection = null;
        Statement statement;

        try {
        connection = SQLUtils.getDBConnection(driver,url,user,password);
        connection.setAutoCommit(true);
        statement = connection.createStatement();
        statement.execute("CREATE SEQUENCE IF NOT EXISTS INTERACTION_SEQ;");
        statement.execute("CREATE TABLE IF NOT EXISTS interaction(id CHAR(18) PRIMARY KEY,sequence BIGINT DEFAULT INTERACTION_SEQ.NEXTVAL,type CHAR(20),startdate TIMESTAMP,enddate TIMESTAMP,timeshift INT DEFAULT 0,xml CLOB, finished BOOLEAN DEFAULT false);");
        statement.execute("CREATE INDEX IF NOT EXISTS date_index_interaction ON interaction(startdate);");
        statement.execute("CREATE INDEX IF NOT EXISTS seq_index_interaction ON interaction(sequence);");
        } catch (SQLException | ClassNotFoundException e) {
        log.error("Error creating new local db, message:{}",e.getMessage());
        } finally {
        if( connection != null ) {
        try {
        connection.close();
        } catch (SQLException e) {
        }
        }
        }
        }


        this method can be used by you in your project, the aim of the method is to create the database if that is empty (do not exist). Just adapt the queries for your database tables, and it will be able to generate sequences, tables, etc. Notice that my SQL syntax is for H2 database, then you need to adapt to your own.



        If you have any further question just let me know, I hope to help you out with that,



        Cheers,



        -Rod







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 10:20









        rod.poli.dinizrod.poli.diniz

        116118




        116118






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444287%2fhow-to-check-if-a-database-is-empty-using-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...