Here you can find the source of executeSqlFile(String filename, Connection connection)
Parameter | Description |
---|---|
filename | the sql file. |
Parameter | Description |
---|---|
Exception | to JUnit |
private static void executeSqlFile(String filename, Connection connection) throws Exception
//package com.java2s; import java.io.FileReader; import java.io.Reader; import java.sql.Connection; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class Main { /**/*from w w w . ja va 2 s .com*/ * Execute the sql statements in a file. * * @param filename the sql file. * @throws Exception to JUnit */ private static void executeSqlFile(String filename, Connection connection) throws Exception { Reader file = new FileReader(filename); char[] buffer = new char[1024]; int retLength = 0; StringBuffer content = new StringBuffer(); while ((retLength = file.read(buffer)) >= 0) { content.append(buffer, 0, retLength); } file.close(); List sqls = new ArrayList(); int lastIndex = 0; //parse the sqls for (int i = 0; i < content.length(); i++) { if (content.charAt(i) == ';') { sqls.add(content.substring(lastIndex, i).trim()); lastIndex = i + 1; } } Statement stmt = null; try { stmt = connection.createStatement(); ; for (int i = 0; i < sqls.size(); i++) { stmt.executeUpdate((String) sqls.get(i)); } } finally { stmt.close(); } } }