of-commons-jdbc/src/main/java/org/openforis/commons/jdbc/ScriptRunner.java
/**
     * Runs an SQL script (read in using the Reader parameter) using the
     * connection passed in
     *
     * @param conn
     *            - the connection to use for the script
     * @param reader
     *            - the source of the script
     * @throws SQLException
     *             if any SQL errors occur
     * @throws IOException
     *             if there is an error reading from the Reader
     */
Runs an SQL script (read in using the Reader parameter) using the
connection passed in

@param conn
- the connection to use for the script
@param reader
- the source of the script
@throws SQLException
if any SQL errors occur
@throws IOException
if there is an error reading from the Reader
Runs an SQL script ( read in using the Reader parameter ) using the connection passed in @param conn - the connection to use for the script @param reader - the source of the script @throws SQLException if any SQL errors occur @throws IOException if there is an error reading from the Reader
private void runScript(Connection conn, Reader reader) throws IOException,
            SQLException {
        StringBuffer command = null;
        try {
            LineNumberReader lineReader = new LineNumberReader(reader);
            String line = null;
            while ((line = lineReader.readLine()) != null) {
                if (command == null) {
                    command = new StringBuffer();
                }
                String trimmedLine = line.trim();
                if (trimmedLine.startsWith("--")) {
                    println(trimmedLine);
                } else if (trimmedLine.length() < 1
                        || trimmedLine.startsWith("//")) {
                } else if (trimmedLine.length() < 1
                        || trimmedLine.startsWith("--")) {
                } else if (!fullLineDelimiter
                        && trimmedLine.endsWith(getDelimiter())
                        || fullLineDelimiter
                        && trimmedLine.equals(getDelimiter())) {
                    Pattern pattern = Pattern.compile(DELIMITER_LINE_REGEX);
                    Matcher matcher = pattern.matcher(trimmedLine);
                    if (matcher.matches()) {
                        setDelimiter(trimmedLine.split(DELIMITER_LINE_SPLIT_REGEX)[1].trim(), fullLineDelimiter);
                        line = lineReader.readLine();
                        if (line == null) {
                            break;
                        }
                        trimmedLine = line.trim();
                    }
                    command.append(line.substring(0, line.lastIndexOf(getDelimiter())));
                    command.append(" ");
                    Statement statement = conn.createStatement();
                    println(command);
                    boolean hasResults = false;
                    if (stopOnError) {
                        hasResults = statement.execute(command.toString());
                    } else {
                        try {
                            statement.execute(command.toString());
                        } catch (SQLException e) {
                            e.fillInStackTrace();
                            printlnError("Error executing: " + command);
                            printlnError(e);
                        }
                    }
                    if (autoCommit && !conn.getAutoCommit()) {
                        conn.commit();
                    }
                    ResultSet rs = statement.getResultSet();
                    if (hasResults && rs != null) {
                        ResultSetMetaData md = rs.getMetaData();
                        int cols = md.getColumnCount();
                        for (int i = 0; i < cols; i++) {
                            String name = md.getColumnLabel(i);
                            print(name + "\t");
                        }
                        println("");
                        while (rs.next()) {
                            for (int i = 0; i < cols; i++) {
                                String value = rs.getString(i);
                                print(value + "\t");
                            }
                            println("");
                        }
                    }
                    command = null;
                    try {
                        statement.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Thread.yield();
                } else {
                    Pattern pattern = Pattern.compile(DELIMITER_LINE_REGEX);
                    Matcher matcher = pattern.matcher(trimmedLine);
                    if (matcher.matches()) {
                        setDelimiter(trimmedLine.split(DELIMITER_LINE_SPLIT_REGEX)[1].trim(), fullLineDelimiter);
                        line = lineReader.readLine();
                        if (line == null) {
                            break;
                        }
                        trimmedLine = line.trim();
                    }
                    command.append(line);
                    command.append(" ");
                }
            }
            if (!autoCommit) {
                conn.commit();
            }
        } catch (SQLException e) {
            e.fillInStackTrace();
            printlnError("Error executing: " + command);
            printlnError(e);
            throw e;
        } catch (IOException e) {
            e.fillInStackTrace();
            printlnError("Error executing: " + command);
            printlnError(e);
            throw e;
        } finally {
            conn.rollback();
            flush();
        }
    }
private void runScript ( Connection conn , Reader reader ) throws IOException , SQLException { StringBuffer command = null ; try { LineNumberReader lineReader = new LineNumberReader ( reader ) ; String line = null ; while ( ( line = lineReader . readLine ( ) ) != null ) { if ( command == null ) { command = new StringBuffer ( ) ; } String trimmedLine = line . trim ( ) ; if ( trimmedLine . startsWith ( "--" ) ) { println ( trimmedLine ) ; } else if ( trimmedLine . length ( ) < 1 || trimmedLine . startsWith ( "//" ) ) { } else if ( trimmedLine . length ( ) < 1 || trimmedLine . startsWith ( "--" ) ) { } else if ( ! fullLineDelimiter && trimmedLine . endsWith ( getDelimiter ( ) ) || fullLineDelimiter && trimmedLine . equals ( getDelimiter ( ) ) ) { Pattern pattern = Pattern . compile ( DELIMITER_LINE_REGEX ) ; Matcher matcher = pattern . matcher ( trimmedLine ) ; if ( matcher . matches ( ) ) { setDelimiter ( trimmedLine . split ( DELIMITER_LINE_SPLIT_REGEX ) [ 1 ] . trim ( ) , fullLineDelimiter ) ; line = lineReader . readLine ( ) ; if ( line == null ) { break ; } trimmedLine = line . trim ( ) ; } command . append ( line . substring ( 0 , line . lastIndexOf ( getDelimiter ( ) ) ) ) ; command . append ( " " ) ; Statement statement = conn . createStatement ( ) ; println ( command ) ; boolean hasResults = false ; if ( stopOnError ) { hasResults = statement . execute ( command . toString ( ) ) ; } else { try { statement . execute ( command . toString ( ) ) ; } catch ( SQLException e ) { e . fillInStackTrace ( ) ; printlnError ( "Error executing: " + command ) ; printlnError ( e ) ; } } if ( autoCommit && ! conn . getAutoCommit ( ) ) { conn . commit ( ) ; } ResultSet rs = statement . getResultSet ( ) ; if ( hasResults && rs != null ) { ResultSetMetaData md = rs . getMetaData ( ) ; int cols = md . getColumnCount ( ) ; for ( int i = 0 ; i < cols ; i ++ ) { String name = md . getColumnLabel ( i ) ; print ( name + "\t" ) ; } println ( "" ) ; while ( rs . next ( ) ) { for ( int i = 0 ; i < cols ; i ++ ) { String value = rs . getString ( i ) ; print ( value + "\t" ) ; } println ( "" ) ; } } command = null ; try { statement . close ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } Thread . yield ( ) ; } else { Pattern pattern = Pattern . compile ( DELIMITER_LINE_REGEX ) ; Matcher matcher = pattern . matcher ( trimmedLine ) ; if ( matcher . matches ( ) ) { setDelimiter ( trimmedLine . split ( DELIMITER_LINE_SPLIT_REGEX ) [ 1 ] . trim ( ) , fullLineDelimiter ) ; line = lineReader . readLine ( ) ; if ( line == null ) { break ; } trimmedLine = line . trim ( ) ; } command . append ( line ) ; command . append ( " " ) ; } } if ( ! autoCommit ) { conn . commit ( ) ; } } catch ( SQLException e ) { e . fillInStackTrace ( ) ; printlnError ( "Error executing: " + command ) ; printlnError ( e ) ; throw e ; } catch ( IOException e ) { e . fillInStackTrace ( ) ; printlnError ( "Error executing: " + command ) ; printlnError ( e ) ; throw e ; } finally { conn . rollback ( ) ; flush ( ) ; } }
Runs an SQL script (read in using the Reader parameter) using the
 connection passed in
Runs an SQL script ( read in using the Reader parameter ) using the connection passed in
// Do nothing // Do nothing // Ignore to workaround a bug in Jakarta DBCP