Java SQL Stored Procedure executeSqlScript(Connection conn, InputStream inputFile)

Here you can find the source of executeSqlScript(Connection conn, InputStream inputFile)

Description

execute Sql Script

License

Open Source License

Declaration

public static void executeSqlScript(Connection conn, InputStream inputFile) 

Method Source Code


//package com.java2s;
/*//from  ww w .j ava  2  s.  c o m
 * Copyright (c) 2005-2012, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *   WSO2 Inc. licenses this file to you under the Apache License,
 *   Version 2.0 (the "License"); you may not use this file except
 *   in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

import java.io.InputStream;
import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void executeSqlScript(Connection conn, InputStream inputFile) {

        // Delimiter
        String delimiter = ";";

        // Create scanner
        Scanner scanner;
        scanner = new Scanner(inputFile).useDelimiter(delimiter);

        // Loop through the SQL file statements
        Statement currentStatement = null;
        while (scanner.hasNext()) {

            // Get statement
            String rawStatement = scanner.next() + delimiter;
            try {
                // Execute statement
                currentStatement = conn.createStatement();
                currentStatement.execute(rawStatement);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                // Release resources
                if (currentStatement != null) {
                    try {
                        currentStatement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                currentStatement = null;
            }
        }
    }
}

Related

  1. executeProcedure(Connection session, String sql)
  2. executeSqlScript(Connection connection, URL scriptUrl)
  3. createTestProcedure()