Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/sql/ScriptExecutor.java


1   /*
2    * ScriptExecutor.java
3    *
4    * Created on 31 dicembre 2001, 18.37
5       Javatools (modified version) - Some useful general classes.
6       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package javatools.sql;
26  
27  import java.lang.*;
28  import java.io.*;
29  import java.sql.*;
30  import javatools.db.*;
31  import javatools.util.*;
32  
33  /** It's an abstract class for use in executing a script for a DBMS.
34   * @author Antonio Petrelli
35   * @version 0.0.2
36   */
37  public class ScriptExecutor {
38  
39      
40      /** Creates new ScriptExecutor */
41      public ScriptExecutor() {
42          sc = new ScriptCatcher();
43          db = null;
44      }
45  
46      /** Sets the name of the DBMS to be used. Not implemented yet!
47       * @param dbmsName The name of DBMS to be used.
48       */    
49      public void setDBMS (String dbmsName) {
50          // Not implemented yet.
51      }
52      
53      /** Returns the name of the DBMS used. Not implemented yet!
54       * @return The name of the DBMS used.
55       */    
56      public String getDBMS () {
57          // Not implemented yet.
58          return "";
59      }
60  
61      /** Sets the database to use.
62       * @param manager The DbManager to use for getting properties.
63       * @param name Name of DBMS.
64       * @param driver Name of JDBC driver class.
65       * @param connectString Connection string to the database.
66       * @param userName User name to connect to DBMS. It must not be included into <CODE>connectString</CODE>!
67       * @param password Password to connect to DBMS. It must not be included into <CODE>connectString</CODE>!
68       */    
69      public void setDatabase(DbManager manager, String name, String driver, String connectString, String userName, String password) {
70          db = new DbDatabase(manager, name, driver, connectString, userName, password);
71          db.setAutoCommit(false);
72      }
73      
74      /** Returns a reference to the DbDatabase object used.
75       * @return The reference to DbDatabase object used
76       * @throws DbException If DbDatabase object is null.
77       */    
78      public DbDatabase getDatabase() throws DbException {
79          if (db != null)
80              return db;
81          else
82              throw new DbException("Database is null.");
83      }
84      
85      /** Loads the script to be executed.
86       * @param scriptFileName Path to the file to be loaded.
87       * @throws FileNotFoundException If the file is not found.
88       */    
89      public void loadScript(java.lang.String scriptFileName) throws FileNotFoundException {
90          try {
91              sc.loadScript(scriptFileName);
92          }
93          catch (FileNotFoundException e) {
94              throw e;
95          }
96      }
97      
98      public void loadScript(InputStream scriptFile) throws IOException {
99          sc.loadScript(scriptFile);
100     }
101     
102     /** Executes the loaded script.
103      * @throws DbException If the execution caused a fault.
104      */    
105     public void execScript() throws DbException {
106         int i, numCommand;
107         DbConnection conn;
108         Connection sqlConn;
109         Statement st;
110         
111         try {
112             conn = db.getNewConnection();
113             sqlConn = conn.getSqlConnection();
114             st = sqlConn.createStatement();
115             numCommand = sc.getNumCommands();
116             try {
117                 st.clearBatch();
118                 for (i=0; i<numCommand; i++)
119                     st.addBatch(sc.getCommandText(i));
120                 st.executeBatch();
121             }
122             catch (SQLException e) {
123                 for (i=0; i < numCommand; i++)
124                     st.executeUpdate(sc.getCommandText(i));
125             }
126             sqlConn.commit();
127             conn.close();
128         }
129         catch (DbException e) {
130             throw e;
131         }
132         catch (SQLException e) {
133             throw new DbException(e.getMessage());
134         }
135     }
136     
137     /** Loads and executes a specified script file.
138      * @param scriptFileName Path to the file to be executed.
139      * @throws DbException If the execution caused a fault.
140      * @throws FileNotFoundException If the file is not found.
141      */    
142     public void execScript(String scriptFileName) throws FileNotFoundException, DbException {
143         try {
144             sc.loadScript(scriptFileName);
145             execScript();
146         }
147         catch (FileNotFoundException e) {
148             throw e;
149         }
150         catch (DbException e) {
151             throw e;
152         }
153     }
154     
155     /** Sets the URL address of the DBMS. Default is <CODE>localhost</CODE>.
156      * @param urlPath The path to the DBMS.
157      */    
158     
159     /** Sets access information for communication with DBMS. Not implemented yet!
160      * @param user Username. You can use an empty string if you want to use no username.
161      * @param pass Password.
162      */    
163     public void setAccess(String user, String pass) {
164         // Not implemted yet.
165     }
166     
167     /** Returns the username. Not implemented yet!
168      * @return Username used. It's empty if no username is specified.
169      */    
170     public String getUserName() {
171         // Not implemented yet.
172         return "";
173     }
174     
175     /** Returns the password used for connection. Not implemented yet!
176      * @return Password used. It's empty if no username is specified.
177      */    
178     public String getPassword() {
179         // Not implemented yet.
180         return "";
181     }
182 
183     /** The name of the database to be used.
184      */    
185     private ScriptCatcher sc;
186     
187     private DbDatabase db;
188 }