1 18 package sync4j.test.tools; 19 20 import java.io.File ; 21 import java.sql.Connection ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 import java.io.BufferedReader ; 27 import java.io.FileReader ; 28 import java.io.StringReader ; 29 30 31 36 public class SQLTools { 37 38 40 50 public static void executeScript(Connection conn, File file) throws IOException , SQLException { 51 52 StringBuffer query = new StringBuffer (); 53 54 BufferedReader br = new BufferedReader (new FileReader (file)); 55 56 String line = null; 57 StringReader lineReader = null; 58 int c = 0; 59 60 while ( (line = br.readLine()) != null ) { 61 if (line.startsWith("--")) { 62 continue; 64 } 65 66 lineReader = new StringReader (line); 67 68 while ( (c = lineReader.read()) != -1) { 69 70 if ( (char)c == ';' ) { 71 executeQuery(conn, query.toString().trim()); 72 query.delete(0, query.length()); 73 } else { 74 query.append( (char)c); 75 } 76 77 } 78 79 lineReader.close(); 80 } 81 82 br.close(); 83 84 } 85 86 95 public static int executeQuery(Connection conn, String query) throws SQLException { 96 Statement stmt = conn.createStatement(); 97 int i = stmt.executeUpdate(query); 98 stmt.close(); 99 return i; 100 } 101 102 103 104 } | Popular Tags |