思路:
1,获取键盘中输入的值
2,根据输入的值,遍历出所有的子目录,将子目录封装成对象,并放入集合中
3. 从集合中取出对象,并存入到数据库
4,测试结果
/**
*创建文件对象
*@author Li Jia Xuan
*@version 1.0
*@since 2012-10-29
*@time 下午03:32:34
*/
public class File1 {
private int i=5;
private String path;
private String last_time;
File1(int i, String path, String last_time) {
//uper();
this.i = i;
this.path = path;
this.last_time = last_time;
}
public int getI() {
return i;
}
public String getPath() {
return path;
}
public String getLast_time() {
return last_time;
}
}
//------------------------------------------------------------------------------------
/**
*创建存入数据库的方法
*@author Li Jia Xuan
*@version 1.0
*@since 2012-10-29
*@time 下午04:45:52
*/
public class TestConn {
private static final String DRIVER_NAMR="oracle.jdbc.driver.OracleDriver";
private static final String URL="jdbc:oracle:thin:@192.168.1.254:1521:orcl";
private static final String USER="lijiaxuan";
private static final String PWD="123456";
public static void test(File1 f) {
Connection conn =null;
PreparedStatement ps=null;
try {
Class.forName(DRIVER_NAMR);
conn = DriverManager.getConnection(URL, USER, PWD);
ps = conn.prepareStatement("insert into file1 values(seq_file.nextval,?,?)");
//ps.setInt(1, f.getI());
ps.setString(1, f.getPath());
ps.setString(2, f.getLast_time());
int i = ps.executeUpdate();
if (i > 0) {
System.out.println("插入成功");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//--------------------------------------------------------------------------------------
1获取控制台的输入
* 2根据输入得到所有路径名,和最后修改时间
* 3.把数据封装到对象中
*@author Li Jia Xuan
*@version 1.0
*@since 2012-10-29
*@time 下午02:08:35
*/
public class DBFile {
/**
* 获取输入的数据
* @return
*/
public String getValue(){
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=null;
try {
while((str=br.readLine())!=null){
//System.out.println(str);
System.out.println("读取完成");
return str;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 获取所有的路径和时间
*/
static
ArrayList<File1> al=new ArrayList<File1>();
public ArrayList<File1> list(String path) {
if (path != null) {
File file = new File(path);
if (file != null) {
File[] file1 = file.listFiles();
if (file1 != null) {
for (File ff : file1) {
if (ff.isDirectory()) {
// long l=ff.lastModified();
// System.out.println(ff.getPath()+String.valueOf(l));
list(ff.getAbsolutePath());
} else {
//
int i=0;
//
i++;
long l = ff.lastModified();
Date d = new Date(l);
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
String str = simple.format(d);
File1 f=new File1(1,ff.getAbsoluteFile().toString(),str);
//
System.out.println(f.getPath());
al.add(f);
System.out.println("存入集合完成");
// TestConn.test(f);
// return al;
}
}
}
}
}
//System.out.println("完成");
return al;
}
}
//--------------------------------------------------------------------------------------
/**
*测试
*@author Li Jia Xuan
*@version 1.0
*@since 2012-10-30
*@time 上午10:25:40
*/
public class Test {
public static void main(String[] args) {
DBFile db=new DBFile();
TestConn t=new TestConn();
String path= db.getValue();
ArrayList<File1> al1=db.list(path);
for (File1 file1 : al1) {
//System.out.println(file1.getPath());
t.test(file1);
}
//两种插入方式遍历集合中元素的方式均可以
System.out.println(al1.size());
//
Iterator<File1> it=al1.iterator();
//
while(it.hasNext()){
//
File1 f=it.next();
//
test(f);
//
}
}
}