Java SQL Batch Execute executeBatch(File file, Connection con, String pluginName)

Here you can find the source of executeBatch(File file, Connection con, String pluginName)

Description

Read a *.sql batch containing SQL statements.

License

Open Source License

Parameter

Parameter Description
file The *.sql batch file
con The connection to the database
pluginName The name of the plugin

Exception

Parameter Description
Exception When file cannot read or statement has wrong syntax

Declaration

public static void executeBatch(File file, Connection con, String pluginName) throws Exception 

Method Source Code


//package com.java2s;
/*//from   w  w  w.  j  av a  2 s.  co  m
 * Copyright (C) 2012 MineStar.de 
 * 
 * This file is part of MineStarLibrary.
 * 
 * MineStarLibrary is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 * 
 * MineStarLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MineStarLibrary.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;

public class Main {
    /**
     * Read a <code>*.sql</code> batch containing SQL statements. This methods read and execute them.
     * 
     * @param file
     *            The *.sql batch file
     * @param con
     *            The connection to the database
     * @param pluginName
     *            The name of the plugin
     * @throws Exception
     *             When file cannot read or statement has wrong syntax
     */
    public static void executeBatch(File file, Connection con, String pluginName) throws Exception {
        executeBatch(new BufferedReader(new FileReader(file)), con, pluginName);
    }

    /**
     * Read a <code>*.sql</code> batch containing SQL statements. This methods read and execute them.
     * 
     * @param filePath
     *            The path to the .sql batch file
     * @param con
     *            The connection to the database
     * @param pluginName
     *            The name of the plugin
     * @throws Exception
     *             When file cannot read or statement has wrong syntax
     */
    public static void executeBatch(String filePath, Connection con, String pluginName) throws Exception {
        executeBatch(new BufferedReader(new FileReader(filePath)), con, pluginName);
    }

    /**
     * Read a <code>*.sql</code> batch containing SQL statements. This methods read and execute them.
     * 
     * @param ressource
     *            The InputStream to the file in the <code>*.jar</code> which can get by <br>
     *            <code>getClass().getResourceAsStream()</code> <br>
     *            Hint: If the target file is in the first package, you have to use <code>/targetFile</code> instead of <code>targetFile</code>!
     * @param con
     *            The connection to the database
     * @param pluginName
     *            The name of the plugin
     * @throws Exception
     *             When file cannot read or statement has wrong syntax
     */
    public static void executeBatch(InputStream ressource, Connection con, String pluginName) throws Exception {
        // Fix for reload
        // When reload is done the ressource is always null
        if (ressource != null)
            executeBatch(new BufferedReader(new InputStreamReader(ressource)), con, pluginName);
    }

    /**
     * Read a <code>*.sql</code> batch containing SQL statements. This methods read and execute them.
     * 
     * @param bReader
     *            The BufferedReader of the resource
     * @param con
     *            The connection to the database
     * @param pluginName
     *            The name of the plugin
     * @throws Exception
     *             When file cannot read or statement has wrong syntax
     */
    private static void executeBatch(BufferedReader bReader, Connection con, String pluginName) throws Exception {
        String line = "";
        StringBuilder sBuilder = new StringBuilder(500);
        while ((line = bReader.readLine()) != null) {
            // ignore empty lines and comments
            if (line.startsWith("#") || line.startsWith("-") || line.isEmpty())
                continue;

            sBuilder.append(line);
            if (line.endsWith(";")) {
                con.createStatement().execute(sBuilder.toString());
                // reset the string builder
                sBuilder.setLength(0);
            }
        }
        bReader.close();
    }
}

Related

  1. executeAsBatch(Connection con, List sqlList)
  2. executeBatch(PreparedStatement prepStmt)
  3. executeBatch(PreparedStatement ps)
  4. executeBatchedUpdate(Connection conn, String sql, int size)
  5. executeBatchedUpdateDataType(Connection conn, String sql)