1 5 package snaq.db; 6 7 import java.io.*; 8 import java.sql.*; 9 import java.util.StringTokenizer ; 10 11 32 public class SQLUpdate 33 { 34 private ConnectionPoolManager cpm; 35 private Connection con; 36 private Statement statement; 37 private String dbName; 38 39 private ByteArrayOutputStream logBuffer; 40 private PrintWriter log; 41 42 43 public SQLUpdate(String db) throws IOException 44 { 45 dbName = db; 46 cpm = ConnectionPoolManager.getInstance(); 47 } 48 49 50 53 private void openConnection(String s) throws SQLException 54 { 55 if (s == null || s.equals("")) 56 { 57 System.out.println("Please specify a database name"); 58 System.exit(1); 59 } 60 con = cpm.getConnection(s); 61 try { statement = con.createStatement(); } 62 catch (SQLException e) {} 63 logBuffer = new ByteArrayOutputStream(); 64 log = new PrintWriter(logBuffer); 65 } 66 67 68 71 private void closeConnection() 72 { 73 try 74 { 75 statement.close(); 76 con.close(); 77 } 78 catch (SQLException e) {} 79 80 log.flush(); 82 log.close(); 83 if (logBuffer.size() > 0) 84 { 85 try 86 { 87 FileOutputStream fos = new FileOutputStream("SQLUpdate.log", true); 88 fos.write(logBuffer.toByteArray()); 89 fos.flush(); 90 fos.close(); 91 } 92 catch (IOException ioe) {} 93 } 94 cpm.release(); 95 } 96 97 98 101 private void doStatement(String sql) 102 { 103 int count = 0; 104 try 105 { 106 count = statement.executeUpdate(sql); 107 System.out.print("."); 108 } 109 catch (SQLException sqle) 110 { 111 System.out.print("x"); 112 log.println(); 113 log.println(sql); 114 log.println(sqle.getMessage()); 115 } 117 } 118 119 120 public static void main(String args[]) throws Exception 121 { 122 if (args == null || args.length < 2) 123 { 124 System.out.println("Usage: java snaq.db.SQLUpdate <database> <text file> [<separator>]"); 125 System.exit(0); 126 } 127 128 String db = args[0]; 129 String file = args[1]; 130 String separator = args.length < 3 ? null : args[2]; 131 132 String contents = null; 134 try 135 { 136 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 137 FileInputStream fis = new FileInputStream(file); 138 byte[] b = new byte[4096]; 139 int n; 140 while ((n = fis.read(b)) != -1) 141 bao.write(b, 0, n); 142 fis.close(); 143 contents = new String (bao.toByteArray()); 144 } 145 catch (IOException ioe) 146 { 147 System.out.println("I/O error with file " + file); 148 System.exit(1); 149 } 150 151 SQLUpdate sql = null; 153 try 154 { 155 sql = new SQLUpdate(db); 156 } 157 catch (IOException ioe) 158 { 159 System.err.println("Unable to create instance of SQLUpdate"); 160 ioe.printStackTrace(); 161 System.exit(1); 162 } 163 sql.openConnection(db); 164 165 if (separator == null) 166 { 167 StringTokenizer st = new StringTokenizer (contents, "\n\r"); 168 while (st.hasMoreTokens()) 169 { 170 String token = st.nextToken().trim(); 171 if (!token.startsWith("#") && !token.equals("")) 172 sql.doStatement(token); 173 } 174 } 175 else 176 { 177 System.out.println("Separator: " + separator); 178 179 StringBuffer sb = new StringBuffer (); 180 StringTokenizer st = new StringTokenizer (contents, "\n\r"); 181 while (st.hasMoreTokens()) 182 { 183 String line = st.nextToken(); 185 186 if (line.startsWith("#") || line.startsWith("--")) 188 { 189 sb.setLength(0); 190 } 191 else 192 { 193 int pos = line.indexOf(separator); 194 if (pos >= 0) 195 { 196 sb.append(line.substring(0, pos)); 197 sql.doStatement(sb.toString()); 198 sb.setLength(0); 199 } 200 else 201 sb.append(line); 202 } 203 } 204 } 205 206 sql.closeConnection(); 207 System.out.println(); 208 } 209 } | Popular Tags |