Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaDB - Checking if a database exists

We created a java application which uses the JavaDB database in Netbeans IDE. We want the program to check every time it starts if the database's tables have already been created, and otherwise create them. How do we do that? thanx

like image 832
smash Avatar asked Aug 09 '10 06:08

smash


1 Answers

I use :

DatabaseMetaData metas;
ResultSet tables;
Statement stat;

m_connexion = DriverManager.getConnection("jdbc:derby:mybase;create=true");
metas = m_connexion.getMetaData();
stat = m_connexion.createStatement();
tables = metas.getTables(m_connexion.getCatalog(), null, "MYTABLE", null);
if (!tables.next())
  stat.execute(
    "CREATE TABLE APP.MYTABLE (" // etc.

... and it's work for me.

like image 59
Istao Avatar answered Oct 22 '22 01:10

Istao