import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class TestDerby  
{
    /**
     * Main test case
     */
    public static void  main(String[] args) throws Exception
    {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        String schema = "VISTA";
        String account = "vista";
        String driver = "jdbc:derby://localhost/vdb3;create=true";
        Connection con = DriverManager.getConnection(driver, account, null);
        for (int i = 0 ; i < 1000 ; i++) {
            String tableName = "test"+i;
            
            if (!TestDerby.tableExists(tableName, con, schema)) {

                Statement stmt = con.createStatement();
                stmt.execute("CREATE TABLE \""+tableName+"\" (\"id_name\" VARCHAR(80), \"id_tag\" INTEGER, \"id_tagType\" VARCHAR(290), \"id_busFmt\" VARCHAR(290), \"id_bpw\" INTEGER, \"description\" VARCHAR(290), \"disp_lowerCritical\" REAL, \"disp_upperCritical\" REAL, \"disp_lowerWarning\" REAL, \"disp_upperWarning\" REAL, \"disp_lowerRange\" REAL, \"disp_upperRange\" REAL, \"disp_equation\" VARCHAR(290), \"disp_format\" VARCHAR(290), \"disp_units\" VARCHAR(290), \"originatorInfo_originator\" VARCHAR(290), \"originatorInfo_lastModified\" VARCHAR(290), PRIMARY KEY (\"id_name\"))");
                String keys[] = getPrimaryKey(con, schema, tableName);
            
            }
            System.out.println("Table: " + i);
        }

    }

    public static boolean tableExists(String pattern, Connection con, String schema)
        throws Exception
    {
        DatabaseMetaData dmd;
        ResultSet res = null;
        boolean exists = false;

        try {
            dmd = con.getMetaData();

            res = dmd
                .getTables(null, schema, pattern, new String[] { "TABLE" });
            exists = res.next();

            return exists;

        } catch (Throwable t) {
        	t.printStackTrace();
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (Throwable t) {
                }
            }
        }
        return false;
    }

    public static String[] getPrimaryKey(Connection dbCon, String schema,
        String table)
        throws Exception
    {
        ResultSet res = null;
        try {
            DatabaseMetaData dmd = dbCon.getMetaData();
            res = dmd.getPrimaryKeys(null, schema, table);
            List list = new ArrayList();

            while (res.next()) {
                list.add(res.getString("COLUMN_NAME"));
            }

            String key[] = new String[list.size()];
            list.toArray(key);
            return key;

        } catch (Throwable t) {
        	t.printStackTrace();
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (Throwable err) {
                    // ignore
                }
            }
        }
        return null;
    }

}
