1 65 66 67 package org.hsqldb.sample; 68 69 import java.io.File ; 70 import java.sql.Connection ; 71 import java.sql.DriverManager ; 72 import java.sql.PreparedStatement ; 73 import java.sql.ResultSet ; 74 import java.sql.SQLException ; 75 import java.sql.Statement ; 76 77 84 class FindFile { 85 86 98 public static void main(String [] arg) { 99 100 try { 102 103 Class.forName("org.hsqldb.jdbcDriver"); 105 106 Connection conn = 111 DriverManager.getConnection("jdbc:hsqldb:testfiles", "sa", 112 ""); 113 114 if (arg.length == 1) { 116 117 listFiles(conn, arg[0]); 120 } else if ((arg.length == 2) && arg[0].equals("-init")) { 121 122 fillFileNames(conn, arg[1]); 125 } else { 126 127 System.out.println("Usage:"); 129 System.out.println("java FindFile -init ."); 130 System.out.println(" Re-create database from directory '.'"); 131 System.out.println("java FindFile name"); 132 System.out.println(" Find files like 'name'"); 133 } 134 135 conn.close(); 137 } catch (Exception e) { 138 139 System.out.println(e); 141 e.printStackTrace(); 142 } 143 } 144 145 147 156 static void listFiles(Connection conn, String name) throws SQLException { 157 158 System.out.println("Files like '" + name + "'"); 159 160 name = name.toUpperCase(); 162 163 Statement stat = conn.createStatement(); 165 166 ResultSet result = stat.executeQuery("SELECT Path FROM Files WHERE " 170 + "UCASE(Path) LIKE '%" + name 171 + "%' ESCAPE ':'"); 172 173 while (result.next()) { 175 176 System.out.println(result.getString(1)); 179 } 180 181 result.close(); 183 } 184 185 187 196 static void fillFileNames(Connection conn, 197 String root) throws SQLException { 198 199 System.out.println("Re-creating the database..."); 200 201 Statement stat = conn.createStatement(); 203 204 try { 206 stat.executeUpdate("DROP TABLE Files"); 207 } catch (SQLException e) { } 209 210 stat.execute("CREATE TABLE Files" 213 + "(Path varchar(255),Name varchar(255))"); 214 215 stat.close(); 217 218 PreparedStatement prep = 220 conn.prepareCall("INSERT INTO Files (Path,Name) VALUES (?,?)"); 221 222 fillPath(root, "", prep); 224 225 prep.close(); 227 System.out.println("Finished"); 228 } 229 230 232 242 static void fillPath(String path, String name, 243 PreparedStatement prep) throws SQLException { 244 245 File f = new File (path); 246 247 if (f.isFile()) { 248 249 prep.clearParameters(); 251 252 prep.setString(1, path); 254 255 prep.setString(2, name); 257 258 prep.execute(); 260 } else if (f.isDirectory()) { 261 if (!path.endsWith(File.separator)) { 262 path += File.separator; 263 } 264 265 String [] list = f.list(); 266 267 for (int i = 0; (list != null) && (i < list.length); i++) { 269 fillPath(path + list[i], list[i], prep); 270 } 271 } 272 } 273 } 274 | Popular Tags |