JAVA遍历路径下所有文件

项目ORACLE的JDBC驱动

代码如下:

package com.gui.file;


import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

public class filenameSave {
	private static TextField text1;
	private static JButton send = new JButton("确定");
	
	public static void main(String[] args) {

		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLayout(new FlowLayout());
		f.setBounds(50, 50, 300, 200);

		text1 = new TextField("", 30);
		text1.setLocation(30, 30);

		
		f.add(text1);
		f.add(send);

		// 在文本框中的事件是:输入文本并回车
		text1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!text1.getText().equals("")) {
					text1.setText("");
				}
			}
		});

		send.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				String strPath = text1.getText();
				fileTraverse filenameList = new fileTraverse();
				DataOperate ds = new DataOperate();
				List<String> list = filenameList.refreshFileList(strPath);
				if(list!=null){
					for(String name :list){
						String  sql = "insert into fileName (filename) values ('" + name+"')" ;
						System.out.println(sql);
						ds.saveDate(sql);
					}
				}

			}});
		
		f.setVisible(true);
	}

}

 

 

package com.gui.file;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

/**
 *数据库操作类
 * @author zhuyuehua
 */
public class DataOperate {
    private String dbDriver = "oracle.jdbc.driver.OracleDriver";
    private String url = "jdbc:oracle:thin:@127.0.0.1:1521:test";
    private String username = "test";
    private String password = "test";
    private Connection con = null;



    public void JDBConnection() {
        try {
            Class.forName(dbDriver).newInstance(); //加载数据库驱动
            System.out.println("数据库加载成功");
        }
        catch (Exception ex) {
            System.out.println("数据库加载失败");
        }
    }

    //创建数据库连接
    public boolean creatConnection() {
        try {
           Class.forName(dbDriver);
           con = DriverManager.getConnection(url, username, password);
            System.out.println("连接数据库成功");
        }catch(ClassNotFoundException ce)
        {
              System.out.println(" 数据库连接失败!\n");
               ce.printStackTrace();
         }
        catch (SQLException e) {
            e.printStackTrace();
        }
    return true;
    }

     //关闭数据库的操作
    public void closeConnection() {
        if (con != null) {
            try {
                con.close();
            System.out.println("关闭数据库成功");
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                con = null;
            }
        }
    }

    //保存数据
    public boolean saveDate(String sql){
        if (con == null) {
            creatConnection();
        }
        try {
            Statement stmt = con.createStatement();
            stmt.executeUpdate(sql);
            stmt.close();
        }catch (SQLException e) {
            return false;
        }finally{
            closeConnection();
        }
        return true;
    }  
}

 

 

 

package com.gui.file;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class fileTraverse {

	private List<String> list = new ArrayList<String>();

	public   List<String> refreshFileList(String strPath) {
		
		File dir = new File(strPath);
		File[] files = dir.listFiles();

		if (files == null)

			return null;

		for (int i = 0; i < files.length; i++) {

			if (files[i].isDirectory()) {

				refreshFileList(files[i].getAbsolutePath());

			} else {

				//String strFileName = files[i].getAbsolutePath().toLowerCase();
				String strFileName = files[i].getName();

				//System.out.println(strFileName);
				
				list.add(strFileName);
			}

		}
		return list;
	}


}

 

你可能感兴趣的:(java)