Link to home
Start Free TrialLog in
Avatar of snajalm
snajalm

asked on

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

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!
Avatar of for_yan
for_yan
Flag of United States of America image


I think this turtorail goes through similar steps:
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/
Avatar of snajalm
snajalm

ASKER

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?!
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there,
This is how you can achieve your goal.
    try
    {
        assume table exists
        write data
    }
    catch(Exception e)
    {
      table didn't exist
       TableFail = true;
    }
    if(tableFail)
    {
      //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 .
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)