1 24 25 package com.mckoi.tools; 26 27 import com.mckoi.util.CommandLine; 28 import com.mckoi.util.ResultOutputUtil; 29 import java.sql.*; 30 import java.io.*; 31 import java.util.Vector ; 32 import java.util.Hashtable ; 33 import java.util.Enumeration ; 34 35 45 46 public class JDBCScriptTool { 47 48 51 private Reader in; 52 53 56 private PrintWriter out; 57 58 61 public JDBCScriptTool(Reader input, PrintWriter output) { 62 this.in = input; 63 this.out = output; 64 } 65 66 70 private String nextQuery() throws IOException { 71 StringBuffer query = new StringBuffer (); 72 int c = in.read(); 73 boolean command = false; 74 while (c != -1) { 75 76 if (command == true || 77 (c != ' ' && c != '\n' && c != '\r' && c != '\t')) { 78 command = true; 79 } 80 81 query.append((char) c); 82 if (c == ';') { 83 return new String (query); 84 } 85 c = in.read(); 86 } 87 if (command == false) { 88 return null; 89 } 90 return new String (query); 91 } 92 93 97 public Connection[] evaluate(Connection connection, String url, 98 String username, String password) throws IOException { 99 103 Statement statement; 104 try { 105 statement = connection.createStatement(); 106 } 107 catch (SQLException e) { 108 out.println("SQL Error creating statement: " + e.getMessage()); 109 return new Connection[] { connection }; 110 } 111 112 Hashtable connections = new Hashtable (); 114 connections.put("default", connection); 115 116 String query = nextQuery(); 117 while (query != null) { 118 119 try { 120 String command = 122 query.substring(0, query.length() - 1).trim().toLowerCase(); 123 if (command.startsWith("switch to connection ")) { 124 String connection_name = command.substring(21); 125 Connection c = (Connection) connections.get(connection_name); 126 if (c == null) { 127 c = DriverManager.getConnection(url, username, password); 128 connections.put(connection_name, c); 129 out.println("Established Connection: " + connection_name); 130 } 131 statement = c.createStatement(); 132 out.println("Switched to Connection: " + connection_name); 133 out.flush(); 134 } 135 else if (command.startsWith("//")) { 136 out.println(); 137 out.println(command); 138 out.flush(); 139 } 140 else { 141 142 out.println(); 143 out.print("> "); 144 out.println(query.trim()); 145 ResultSet result_set = statement.executeQuery(query); 146 ResultOutputUtil.formatAsText(result_set, out); 147 out.flush(); 148 } 149 } 150 catch (SQLException e) { 151 out.println("SQL Error running query (\n" + query + "\n)\nError: " + 152 e.getMessage()); 153 out.println("Error code: " + e.getErrorCode()); 154 } 156 out.flush(); 157 158 query = nextQuery(); 160 } 161 162 Enumeration e = connections.elements(); 163 Vector v = new Vector (); 164 while (e.hasMoreElements()) { 165 v.addElement(e.nextElement()); 166 } 167 168 Connection[] arr = new Connection[v.size()]; 169 for (int i = 0; i < arr.length; ++i) { 170 arr[i] = (Connection) v.elementAt(i); 171 } 172 return arr; 173 174 } 175 176 177 179 182 private static void printSyntax() { 183 System.out.println( 184 "JDBCScriptTool [-jdbc JDBC_Driver_Class] [-url JDBC_URL] \n" + 185 " -u username -p password \n" + 186 " [-in Input_SQL_File] [-out Output_Result_File] \n" + 187 "\n" + 188 " If -in or -out are not specified then the tool uses System.in \n" + 189 " and System.out respectively.\n"); 190 } 191 192 195 public static void main(String [] args) { 196 CommandLine cl = new CommandLine(args); 197 198 String driver = cl.switchArgument("-jdbc", "com.mckoi.JDBCDriver"); 199 String url = cl.switchArgument("-url", ":jdbc:mckoi:"); 200 String username = cl.switchArgument("-u"); 201 String password = cl.switchArgument("-p"); 202 final String input_file = cl.switchArgument("-in"); 203 final String output_file = cl.switchArgument("-out"); 204 205 if (username == null) { 206 System.out.println("Please provide a username"); 207 System.out.println(); 208 printSyntax(); 209 System.exit(1); 210 } 211 else if (password == null) { 212 System.out.println("Please provide a password"); 213 System.out.println(); 214 printSyntax(); 215 System.exit(1); 216 } 217 218 Reader reader = null; 219 PrintWriter writer = null; 220 221 try { 222 if (input_file == null) { 223 reader = new InputStreamReader(System.in); 224 } 225 else { 226 reader = new BufferedReader(new FileReader(input_file)); 227 } 228 229 if (output_file == null) { 230 writer = new PrintWriter( 231 new BufferedWriter(new OutputStreamWriter(System.out))); 232 } 233 else { 234 writer = new PrintWriter( 235 new BufferedWriter(new FileWriter(output_file))); 236 } 237 238 } 239 catch (IOException e) { 240 System.err.println("IO Error: " + e.getMessage()); 241 printSyntax(); 242 System.exit(1); 243 } 244 245 JDBCScriptTool tool = new JDBCScriptTool(reader, writer); 247 248 Connection connection = null; 250 try { 251 writer.println("Using JDBC Driver: " + driver); 252 253 Class.forName(driver).newInstance(); 255 256 connection = DriverManager.getConnection(url, username, password); 258 259 writer.println("Connection established to: " + url); 260 writer.flush(); 261 262 } 263 catch (ClassNotFoundException e) { 264 writer.println("JDBC Driver not found."); 265 printSyntax(); 266 System.exit(1); 267 } 268 catch (Exception e) { 269 e.printStackTrace(); 270 printSyntax(); 271 System.exit(1); 272 } 273 274 try { 275 276 if (input_file != null) { 277 System.out.println("Script input from: " + input_file); 278 } 279 280 Connection[] conl = tool.evaluate(connection, url, username, password); 282 283 reader.close(); 285 writer.println(); 286 writer.println(" --- FINISHED"); 287 writer.close(); 288 289 295 for (int i = 0; i < conl.length; ++i) { 296 conl[i].close(); 297 } 298 300 if (output_file != null) { 301 System.out.println("Script output to: " + output_file); 302 } 303 304 } 305 catch (IOException e) { 306 System.err.println("IO Error: " + e.getMessage()); 307 e.printStackTrace(System.err); 308 System.exit(1); 309 } 310 catch (SQLException e) { 311 System.err.println("SQL Error: " + e.getMessage()); 312 e.printStackTrace(System.err); 313 System.exit(1); 314 } 315 316 } 317 318 319 } 320 | Popular Tags |