`

键盘输入目录,将子目录和最后修改时间存入数据库

阅读更多

思路:

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);
// }
}
}

分享到:
评论

相关推荐

    Delphi开发范例宝典目录

    实例272 和服务器时间同步 350 实例273 取得网络中的SQL服务器名 351 8.5 数据库维护 352 实例274 数据库登录配置 352 实例275 SQL Server数据库的备份和恢复 353 实实276 数据库附加、分离 355 实例277...

    C#程序开发范例宝典(第2版).part13

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

    C#程序开发范例宝典(第2版).part08

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

    C#程序开发范例宝典(第2版).part02

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

    C#程序开发范例宝典(第2版).part12

    精选570个典型范例,全面覆盖实用和热点技术,涉及面广,实用性强源于实际项目开发,帮助读者短时间掌握更多实用技术,提高编程水平范例经过精心编排,重点、难点突出,易学易懂书后附录提供快速索引,即查、即学、...

    C#.net_经典编程例子400个

    89 2.8 TreeView控件应用 91 实例073 将数据库数据显示到树视图中 91 实例074 用TreeView控件制作导航界面 93 实例075 用TreeView控件遍历磁盘目录 94 实例076 TreeView控件在数据库中的...

    cmd操作命令和linux命令大全收集

    date /t 、 time /t 使用此参数即“DATE/T”、“TIME/T”将只显示当前日期和时间,而不必输入新日期和时间 set 指定环境变量名称=要指派给变量的字符 设置环境变量 set 显示当前所有的环境变量...

    会计理论考试题

    1.计算机的常用输入设备有键盘和鼠标器。( Y ) 2.软盘要远离如电话、录音设备等热源、磁源。( Y ) 3.硬盘格式化分二个阶段,低级格式化和高级格式化。( N ) 4.硬盘格式化分三个阶段,其先后顺序为低级格式化、...

    C#编程经验技巧宝典

    103 &lt;br&gt;0167 锁定文本框内的文本 103 &lt;br&gt;0168 使用Message.Show输出用户信息 104 &lt;br&gt;5.3 图片数据处理技巧 104 &lt;br&gt;0169 如何将图片存入数据库 104 &lt;br&gt;0170 如何将图片从数据库中读取...

    Linux操作系统基础教程

    者的使用活动基本不受限制(只要你不将它用于商业目的),而不必像使用微软产品是那样, 2需要为购满许可证付出高价还要受到系统安装数量的限制。我在讲义的最后面附有 GPL 的 非官方中文译稿。目前 Linux 中国的...

Global site tag (gtag.js) - Google Analytics