KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > tools > ij


1 /*
2
3    Derby - Class org.apache.derby.tools.ij
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.tools;
23
24 import org.apache.derby.iapi.services.info.JVMInfo;
25 import org.apache.derby.iapi.tools.i18n.LocalizedInput;
26 import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
27 import org.apache.derby.iapi.tools.i18n.LocalizedResource;
28
29 import org.apache.derby.impl.tools.ij.Main;
30 import org.apache.derby.impl.tools.ij.utilMain;
31
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.PrintStream JavaDoc;
36 import java.io.UnsupportedEncodingException JavaDoc;
37 import java.sql.Connection JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 /**
41     
42     ij is Derby's interactive JDBC scripting tool.
43     It is a simple utility for running scripts against a Derby database.
44     You can also use it interactively to run ad hoc queries.
45     ij provides several commands for ease in accessing a variety of JDBC features.
46     <P>
47
48     To run from the command line enter the following:
49     <p>
50     java [options] org.apache.derby.tools.ij [arguments]
51     <P>
52     ij is can also be used with any database server that supports a JDBC driver.
53 */

54 public class ij {
55
56   /**
57     @exception IOException thrown if cannot access input or output files.
58    */

59   static public void main(String JavaDoc[] args) throws IOException JavaDoc {
60
61       /* We decide which verion of ij (2.0 or 4.0) to
62        * load based on the same criteria that the JDBC driver
63        * uses.
64        */

65       if (JVMInfo.JDK_ID == JVMInfo.J2SE_13)
66       {
67           Main.main(args);
68       }
69       else
70       {
71           org.apache.derby.impl.tools.ij.Main14.main(args);
72       }
73   }
74   
75   /**
76    * Run a SQL script from an InputStream and write
77    * the resulting output to the provided PrintStream.
78    * SQL commands are separated by a semi-colon ';' character.
79    *
80    * @param conn Connection to be used as the script's default connection.
81    * @param sqlIn InputStream for the script.
82    * @param inputEncoding Encoding of the script.
83    * @param sqlOut OutputStream for the script's output
84    * @param outputEncoding Output encoding to use.
85    * @return Number of SQLExceptions thrown during the execution, -1 if not known.
86    * @throws UnsupportedEncodingException
87    */

88   public static int runScript(
89           Connection JavaDoc conn,
90           InputStream JavaDoc sqlIn,
91           String JavaDoc inputEncoding,
92           OutputStream JavaDoc sqlOut,
93           String JavaDoc outputEncoding)
94           throws UnsupportedEncodingException JavaDoc
95   {
96       LocalizedOutput lo =
97           outputEncoding == null ?
98                   LocalizedResource.getInstance().
99                     getNewOutput(sqlOut)
100                  :
101                   LocalizedResource.getInstance().
102                     getNewEncodedOutput(sqlOut, outputEncoding);
103
104       Main ijE;
105       if (JVMInfo.JDK_ID == JVMInfo.J2SE_13)
106       {
107           ijE = new Main(false);
108       }
109       else
110       {
111           // temp - allow ij to continue to work under jdk131
112
// will resolve as part of DEBRY-1609
113
// jdk13 gets error loading Main14 due to the
114
// class now being built with the jdk14 target flag.
115
// ijE = new org.apache.derby.impl.tools.ij.Main14(false);
116
ijE = new Main(false);
117       }
118       
119       LocalizedInput li = LocalizedResource.getInstance().
120                 getNewEncodedInput(sqlIn, inputEncoding);
121       
122       utilMain um = ijE.getutilMain(1, lo);
123
124       return um.goScript(conn, li);
125   }
126
127   private ij() { // no instances allowed
128
}
129   
130   public static String JavaDoc getArg(String JavaDoc param, String JavaDoc[] args)
131   {
132       return org.apache.derby.impl.tools.ij.util.getArg(param, args);
133   }
134
135   public static void getPropertyArg(String JavaDoc[] args) throws IOException JavaDoc
136   {
137       org.apache.derby.impl.tools.ij.util.getPropertyArg(args);
138   }
139
140   public static java.sql.Connection JavaDoc startJBMS()
141       throws java.sql.SQLException JavaDoc, IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc, InstantiationException JavaDoc
142   {
143         return org.apache.derby.impl.tools.ij.util.startJBMS();
144   }
145 }
146
Popular Tags