001: /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the Hypersonic SQL Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: *
030: * This software consists of voluntary contributions made by many individuals
031: * on behalf of the Hypersonic SQL Group.
032: *
033: *
034: * For work added by the HSQL Development Group:
035: *
036: * Copyright (c) 2001-2005, The HSQL Development Group
037: * All rights reserved.
038: *
039: * Redistribution and use in source and binary forms, with or without
040: * modification, are permitted provided that the following conditions are met:
041: *
042: * Redistributions of source code must retain the above copyright notice, this
043: * list of conditions and the following disclaimer.
044: *
045: * Redistributions in binary form must reproduce the above copyright notice,
046: * this list of conditions and the following disclaimer in the documentation
047: * and/or other materials provided with the distribution.
048: *
049: * Neither the name of the HSQL Development Group nor the names of its
050: * contributors may be used to endorse or promote products derived from this
051: * software without specific prior written permission.
052: *
053: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
054: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
055: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
056: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
057: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
058: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
059: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
060: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
061: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
062: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
063: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
064: */
065:
066: package org.hsqldb.sample;
067:
068: import java.io.File;
069: import java.sql.Connection;
070: import java.sql.DriverManager;
071: import java.sql.PreparedStatement;
072: import java.sql.ResultSet;
073: import java.sql.SQLException;
074: import java.sql.Statement;
075:
076: /**
077: * Extract a directory tree and store in an HSQLDB database.
078: *
079: * @author Thomas Mueller (Hypersonic SQL Group)
080: * @version 1.7.0
081: * @since Hypersonic SQL
082: */
083: class FindFile {
084:
085: /**
086: * Extracts a directory tree and stores it ina HSQLDB database.<br>
087: * Usage:<p>
088: * <pre>
089: * java org.hsqldb.sample.FindFile -init .
090: * Re-create database from directory '.'
091: * java org.hsqldb.sample.FindFile name
092: * Find files like 'name'
093: * </pre>
094: *
095: * @param arg
096: */
097: public static void main(String[] arg) {
098:
099: // Exceptions may occur
100: try {
101:
102: // Load the HSQL Database Engine JDBC driver
103: Class.forName("org.hsqldb.jdbcDriver");
104:
105: // Connect to the database
106: // It will be create automatically if it does not yet exist
107: // 'testfiles' in the URL is the name of the database
108: // "sa" is the user name and "" is the (empty) password
109: Connection conn = DriverManager.getConnection(
110: "jdbc:hsqldb:testfiles", "sa", "");
111:
112: // Check the command line parameters
113: if (arg.length == 1) {
114:
115: // One parameter:
116: // Find and print the list of files that are like this
117: listFiles(conn, arg[0]);
118: } else if ((arg.length == 2) && arg[0].equals("-init")) {
119:
120: // Command line parameters: -init pathname
121: // Init the database and fill all file names in
122: fillFileNames(conn, arg[1]);
123: } else {
124:
125: // Display the usage info
126: System.out.println("Usage:");
127: System.out.println("java FindFile -init .");
128: System.out
129: .println(" Re-create database from directory '.'");
130: System.out.println("java FindFile name");
131: System.out.println(" Find files like 'name'");
132: }
133:
134: // Finally, close the connection
135: conn.close();
136: } catch (Exception e) {
137:
138: // Print out the error message
139: System.out.println(e);
140: e.printStackTrace();
141: }
142: }
143:
144: // Search in the database and list out files like this
145:
146: /**
147: * Method declaration
148: *
149: *
150: * @param conn
151: * @param name
152: *
153: * @throws SQLException
154: */
155: static void listFiles(Connection conn, String name)
156: throws SQLException {
157:
158: System.out.println("Files like '" + name + "'");
159:
160: // Convert to upper case, so the search is case-insensitive
161: name = name.toUpperCase();
162:
163: // Create a statement object
164: Statement stat = conn.createStatement();
165:
166: // Now execute the search query
167: // UCASE: This is a case insensitive search
168: // ESCAPE ':' is used so it can be easily searched for '\'
169: ResultSet result = stat
170: .executeQuery("SELECT Path FROM Files WHERE "
171: + "UCASE(Path) LIKE '%" + name
172: + "%' ESCAPE ':'");
173:
174: // Moves to the next record until no more records
175: while (result.next()) {
176:
177: // Print the first column of the result
178: // could use also getString("Path")
179: System.out.println(result.getString(1));
180: }
181:
182: // Close the ResultSet - not really necessary, but recommended
183: result.close();
184: }
185:
186: // Re-create the database and fill the file names in
187:
188: /**
189: * Method declaration
190: *
191: *
192: * @param conn
193: * @param root
194: *
195: * @throws SQLException
196: */
197: static void fillFileNames(Connection conn, String root)
198: throws SQLException {
199:
200: System.out.println("Re-creating the database...");
201:
202: // Create a statement object
203: Statement stat = conn.createStatement();
204:
205: // Try to drop the table
206: try {
207: stat.executeUpdate("DROP TABLE Files");
208: } catch (SQLException e) { // Ignore Exception, because the table may not yet exist
209: }
210:
211: // For compatibility to other database, use varchar(255)
212: // In HSQL Database Engine, length is unlimited, like Java Strings
213: stat.execute("CREATE TABLE Files"
214: + "(Path varchar(255),Name varchar(255))");
215:
216: // Close the Statement object, it is no longer used
217: stat.close();
218:
219: // Use a PreparedStatement because Path and Name could contain '
220: PreparedStatement prep = conn
221: .prepareCall("INSERT INTO Files (Path,Name) VALUES (?,?)");
222:
223: // Start with the 'root' directory and recurse all subdirectories
224: fillPath(root, "", prep);
225:
226: // Close the PreparedStatement
227: prep.close();
228: System.out.println("Finished");
229: }
230:
231: // Fill the file names, using the PreparedStatement
232:
233: /**
234: * Method declaration
235: *
236: *
237: * @param path
238: * @param name
239: * @param prep
240: *
241: * @throws SQLException
242: */
243: static void fillPath(String path, String name,
244: PreparedStatement prep) throws SQLException {
245:
246: File f = new File(path);
247:
248: if (f.isFile()) {
249:
250: // Clear all Parameters of the PreparedStatement
251: prep.clearParameters();
252:
253: // Fill the first parameter: Path
254: prep.setString(1, path);
255:
256: // Fill the second parameter: Name
257: prep.setString(2, name);
258:
259: // Its a file: add it to the table
260: prep.execute();
261: } else if (f.isDirectory()) {
262: if (!path.endsWith(File.separator)) {
263: path += File.separator;
264: }
265:
266: String[] list = f.list();
267:
268: // Process all files recursivly
269: for (int i = 0; (list != null) && (i < list.length); i++) {
270: fillPath(path + list[i], list[i], prep);
271: }
272: }
273: }
274: }
|