1 13 14 package org.ejbca.util; 15 16 import java.io.BufferedReader ; 17 import java.io.File ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileReader ; 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 import java.sql.Timestamp ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 30 import org.apache.log4j.Logger; 31 32 35 public class SqlExecutor { 36 static Logger log = Logger.getLogger(SqlExecutor.class); 37 38 private Connection con = null; 39 private int commands = 0; 40 private int errors = 0; 41 private boolean continueOnSqlError = false; 42 public Connection getConnection() { 43 return this.con; 44 } 45 public void setContinueOnSqlError(boolean cont) { 46 this.continueOnSqlError = cont; 47 } 48 public int getErrors() { 49 return this.errors; 50 } 51 55 public SqlExecutor(Connection connection, boolean continueOnSQLError) { 56 log.debug("> SqlExecutor(" + connection + "," + continueOnSQLError+ ")"); 57 con = connection; 58 this.continueOnSqlError = continueOnSQLError; 62 log.debug("< SqlExecutor()"); 63 } 64 65 71 public int runCommand(String command) throws SQLException { 72 log.debug("> runCommand: " + command); 73 StringBuffer tmp = new StringBuffer (command); 74 int res = executeCommand(tmp); 75 log.debug(++commands + " commands executed with " + errors + " errors"); 76 commands = 0; 77 errors = 0; 78 log.debug("< runCommand"); 79 return res; 80 } 81 public void runCommandFile(File file) throws SQLException , FileNotFoundException , IOException { 82 log.debug("> runCommandFile: " + file.getPath()); 83 Reader rdr = new FileReader (file); 84 runCommands(rdr); 85 log.debug("< runCommandFile()"); 86 } 87 88 95 public void runCommands(Reader rdr) throws SQLException , IOException { 96 log.debug(">runCommands"); 97 BufferedReader br = new BufferedReader (rdr); 98 Timestamp start = new Timestamp (System.currentTimeMillis()); 99 try { 100 String temp; 101 StringBuffer strBuf = new StringBuffer (); 102 commands = 0; 103 List list = new LinkedList (); 104 while ((temp = br.readLine()) != null) { 105 if (!temp.startsWith("#")) { list.add(temp); 107 } 108 if (!temp.endsWith(";")) 109 continue; 110 } 111 Iterator it = list.iterator(); 112 while (it.hasNext()) { 113 temp = (String ) it.next(); 114 temp = temp.trim(); 115 if (temp.length() != 0) { 116 strBuf.append(temp); 117 if (temp.endsWith(";")) { 118 char ch = ' '; 120 strBuf.setCharAt(strBuf.length() - 1, ch); 121 executeCommand(strBuf); 122 commands++; 123 strBuf = new StringBuffer (); 124 } else { 125 strBuf.append(" "); 127 } 128 } 129 } 130 } finally { 131 if (br != null) { br.close(); } 132 } 133 Timestamp stop = new Timestamp (System.currentTimeMillis()); 134 log.debug("Execution started: " + start.toString()); 135 log.debug("Execution stopped: " + stop.toString()); 136 log.debug(commands + " commands executed with " + errors + " errors"); 137 commands = 0; 138 errors = 0; 139 log.debug("<runCommands"); 140 } 141 142 149 private int executeCommand(StringBuffer command) throws SQLException { 150 log.debug("> executeCommand: " + command); 151 Statement stmt = null; 152 int res = 0; 153 try { 154 stmt = con.createStatement(); 155 String sql = command.toString(); 156 res = stmt.executeUpdate(sql); 157 } catch (SQLException exception) { 158 log.error("Exception: " + exception.getMessage() + ", sql: "+ command.toString()); 159 if (!this.continueOnSqlError) { 160 throw exception; 161 } 162 errors++; 163 } finally { 164 if (stmt != null) { 165 stmt.close(); 166 } 167 } 168 log.debug("< executeCommand"); 169 return res; 170 } 172 173 194 } 195 | Popular Tags |