• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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!
0
snajalm
Asked:
snajalm
2 Solutions
 
for_yanCommented:

I think this turtorail goes through similar steps:
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/
0
 
snajalmAuthor Commented:
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?!
0
 
for_yanCommented:
That example shows how to set up connection, etc.

This question deals how to check existence once you set up the connection:
http://stackoverflow.com/questions/584497/apache-derby-check-database-already-created
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Plk_In_EECommented:
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 .
0
 
CEHJCommented:
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";

See http://db.apache.org/derby/integrate/plugin_help/derby_app.html
0
 
CEHJCommented:
:)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now