db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kaoullas, Adamos D" <a.kaoul...@imperial.ac.uk>
Subject RE: Does derby support .sql files?
Date Wed, 16 Feb 2005 10:18:37 GMT
I am using Derby in embedded mode within my java application, and
creating my tables using sql files. I use a static helper function to
sort the sql statements into an array of strings - basically using the
';' char as a delimiter to sort into different statements, then execute
the statements as normally using a Connection and Statement objects. I
have included the code below.


    /**
     * Get SQL Statements from a file. Assumes that a semi-colon marks
the end
     * of a statement, and that semi-colon is only used to terminate a
     * statement.
     * 
     * @param file,
     *            the file to read from
     * @return array of strings containing statements.
     * @throws IOException
     */
    public static String[] getSQLStatementsFromFile(File file)
            throws IOException {
        final StringWriter buffer = new StringWriter();
        Reader reader = new BufferedReader(new FileReader(file));

        ArrayList statements = new ArrayList();
        // the unicode value for ';' is 59
        final int semi_colon = 59;
        // Suck up the contents of the temporary file
        for (int read = reader.read(); read != -1; read = reader.read())
{
            if (read == semi_colon) {
                statements.add(buffer.toString());
                buffer.getBuffer().delete(0,
buffer.getBuffer().length());
            } else {
                buffer.write(read);
            }
        }
        return (String[]) statements.toArray(new
String[statements.size()]);
    }

    /**
     * Execute sql statements from array of Strings.
     * 
     * @param conn
     * @param statements
     * @return
     * @throws SQLException
     */
    public static int executeSQL(Connection conn, String[] statements)
        throws SQLException {

        int count = 0;
        conn.setAutoCommit(false);
        // create an sql statement and create the tables,.
        Statement stmt = conn.createStatement();
        for (int i = 0; i < statements.length; i++) {
            String sql_statement = statements[i];
            count += stmt.executeUpdate(sql_statement);
        }
        conn.commit();
        conn.setAutoCommit(true);
        
        return count;
    }


Regards,

Adam
-----Original Message-----
From: Ruta Kadhe [mailto:ruta.kadhe@gmail.com] 
Sent: 16 February 2005 06:25
To: derby-user@db.apache.org
Subject: Does derby support .sql files?

Hi,
Consider Derby in embeded mode.
When database is to be created in embeded mode, is their any support in
Derby to run .sql file which has Database Creation scripts?

If not, is there any alternative method by which we can read the scripts
and run those scripts to create database?

Thanks & Regards,
Ruta

Mime
View raw message