Here you can find the source of executeSQL(Connection connection, String file)
Parameter | Description |
---|---|
connection | the connection. |
file | the file. |
Parameter | Description |
---|---|
Exception | to JUnit. |
private static void executeSQL(Connection connection, String file) throws Exception
//package com.java2s; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.sql.Connection; import java.sql.Statement; public class Main { /**/*from w w w.ja v a 2s .co m*/ * Executes the SQL statements from file. * * @param connection * the connection. * @param file * the file. * * @throws Exception * to JUnit. */ private static void executeSQL(Connection connection, String file) throws Exception { String[] values = readFile(file).split(";"); Statement statement = connection.createStatement(); try { for (int i = 0; i < values.length; i++) { String sql = values[i].trim(); if ((sql.length() != 0) && (!sql.startsWith("#"))) { statement.executeUpdate(sql); } } } finally { statement.close(); } } /** * Reads file to a string. * * @param fileName * the name of the file to read. * * @return a string represents the content. * * @throws IOException * if any IO error occurs. */ private static String readFile(String fileName) throws IOException { Reader reader = new FileReader(fileName); try { StringBuilder sb = new StringBuilder(); char[] buffer = new char[1024]; int k = 0; while ((k = reader.read(buffer)) != -1) { sb.append(buffer, 0, k); } return sb.toString().replace("\r\n", "\n"); } finally { try { reader.close(); } catch (IOException ioe) { // Ignore } } } }