Creating a Java DB database and associated tables in main checking to see if they exist!

Posted on 2011-10-11
Last Modified: 2012-05-12
I'd like my application to have a little code in main so that it can create a Java DB connection checking to see if the database and the associate tables exist, if not create the database and the tables in it.  If you could provide a sample code, it'd be just as great!
Question by:snajalm
    LVL 47

    Expert Comment


    I think this turtorail goes through similar steps:

    Author Comment

    Thanks "for_yan" but I could not find anything there that checks for an existing database before creating one!  Could you please be a little more specific?!
    LVL 47

    Assisted Solution

    That example shows how to set up connection, etc.

    This question deals how to check existence once you set up the connection:
    LVL 5

    Expert Comment

    Hi there,
    This is how you can achieve your goal.
            assume table exists
            write data
        catch(Exception e)
          table didn't exist
           TableFail = true;
          //if we get in here we know the table doesn't exist so create the table

    There is a problem with the above approach, even if there is some other exception it will land up in table fail condition considering table does not exists.

    Because SQLExceptions get thrown for EVERYTHING. if bad sql is sent you get an sql exception, if a column isn't found you get an sqlexceptionl, if table doesn't exist you get an sql exception .
    LVL 86

    Accepted Solution

    Simply use create=true in the url string, e.g.

    private static String dbURL = "jdbc:derby://localhost:1527/myDB;create=true;user=me;password=mine";

    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now