JavaDB - checking for a database

We created a Java application that uses the JavaDB database in the Netbeans IDE. We want the program to be checked every time it starts, if the database tables are already created and are otherwise created. How do we do this? thanks

+6
derby netbeans javadb
source share
2 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 works for me.

+6
source share

Istao test for table existence did not work for me with Derby. The table was not found, even if it was created earlier. What is missing, you need to specify TABLE_SCHEM as "APP" and then set the table type to include "TABLE". Using null may work in previous versions, but using Derby 10.12 does not find a previously created table with the specified parameters equal to zero.

 Connection conn = DriverManager.getConnection(DB_PROTO + DB_NAME + ";create=true"); DatabaseMetaData metas = conn.getMetaData(); ResultSet tables = metas.getTables(conn.getCatalog(), "APP", TABLE_NODES, new String[] {"TABLE"}); if (!tables.next()) { Statement stat = conn.createStatement(); stat.execute("create table " + ... 

Hope this helps someone else.

0
source share

All Articles