1 24 25 package org.objectweb.cjdbc.console.text.commands.sqlconsole; 26 27 import java.io.BufferedReader ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.sql.Connection ; 31 32 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate; 33 import org.objectweb.cjdbc.console.text.ConsoleException; 34 import org.objectweb.cjdbc.console.text.commands.ConsoleCommand; 35 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseConsole; 36 37 42 public class Load extends ConsoleCommand 43 { 44 45 50 public Load(VirtualDatabaseConsole module) 51 { 52 super(module); 53 } 54 55 58 public void parse(String commandText) throws IOException , ConsoleException 59 { 60 String fileName = commandText.trim(); 61 if ("".equals(fileName)) 62 { 63 console.printError(getUsage()); 64 return; 65 } 66 load(commandText.trim()); 67 } 68 69 75 public void load(String fileName) 76 { 77 Connection connection = ((VirtualDatabaseConsole)module).getConnection(); 78 79 BufferedReader file = null; 80 try 81 { 82 file = new BufferedReader (new FileReader (fileName)); 83 } 84 catch (Exception e) 85 { 86 console.printError( 87 ConsoleTranslate.get("sql.command.load.file.error", e), e); 88 return; 89 } 90 91 console.println(ConsoleTranslate.get("sql.command.loading.file", fileName)); 92 try 93 { 94 String request; 95 96 while ((request = file.readLine()) != null) 97 { 98 request = request.trim(); 99 console.println(request); 100 101 if (request.equalsIgnoreCase("begin")) 102 connection.setAutoCommit(false); 103 else if (request.equalsIgnoreCase("commit")) 104 connection.commit(); 105 else if (request.equalsIgnoreCase("rollback")) 106 connection.rollback(); 107 else 108 { ((VirtualDatabaseConsole)module).execSQL(request, false); 110 } 111 } 112 } 113 catch (Exception e) 114 { 115 console.printError(ConsoleTranslate.get("sql.command.load.execute.error", 116 e), e); 117 } 118 finally 119 { 120 try 121 { 122 file.close(); 123 } 124 catch (IOException ignore) 125 { 126 } 127 } 128 } 129 130 133 public String getCommandName() 134 { 135 return "load"; 136 } 137 138 141 public String getCommandParameters() 142 { 143 return ConsoleTranslate.get("sql.command.load.params"); 144 } 145 146 149 public String getCommandDescription() 150 { 151 return ConsoleTranslate.get("sql.command.load.description"); 152 } 153 } | Popular Tags |