您的位置:首页 > 数据库 > Oracle

java 删除oracle指定表的数据同时清除对应存储的数据

2011-12-27 16:18 423 查看
首先下载ojdbc14.jar,导入到项目工程下,注:本例中是先把储存挂盘到本地然后调用JAVA的FILE进行操作的

import java.io.File;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

/**

 * @author liuqiong

 *

 */

public class Connect {

    /**

     * @param args

     */

    public static void main(String[] args) {

        try {

            try {

                //newsDelete();// 删除news表的方法

                 programDelete();//删除PROGRAM表的方法

            } catch (SQLException e) {

                e.printStackTrace();

            }

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }

    }

    public static void newsDelete() throws ClassNotFoundException, SQLException {

        Class.forName("oracle.jdbc.driver.OracleDriver");// 加载连接数据库的驱动

        String url = "jdbc:oracle:thin:@数据库的IP:端口号:服务器名";

        String user = "用户名";

        String password = "密码";

        Connection con = DriverManager.getConnection(url, user, password);// 创建连接

        String str = "delete  from  resources_info_news  where  play_time>to_date('20111023 0010','yyyymmdd hh24mi')and play_time<to_date('20111023 0015','yyyymmdd hh24mi')";

        String str2 = "select path_info from  resources_info_news  where resourcs_type not in ('TS','SJPG','JPG','AVI','MJPG','MTG','XMLTMF','MP4','MP4_MAIN','PTMF')  and play_time>to_date('20111023 0010','yyyymmdd hh24mi')and play_time<to_date('20111023 0015','yyyymmdd
hh24mi')";

        PreparedStatement stmt = con.prepareStatement(str);// 创建prepareStatement对象并实例化,返回结果集给ResultSet

        PreparedStatement stmt2 = con.prepareStatement(str2);

        ResultSet rs = stmt2.executeQuery();

        String child = null;

        // File fi = new File("z:\\CustomerCache\\", child);

        //int i = stmt.executeUpdate();

        while (rs.next()) {// 遍历rs结果集,因为在本例中有些数据只需清除数据库,而无需删除对应存储的数据,这些可在定义的两个sql语句中可看出

            child = rs.getString("path_info");

            File fi = new File("e:\\CustomerCache\\", child);

            deletFiles(fi);// 调用删除文件的方法

        }

        

        int i = stmt.executeUpdate();

        rs.close();

        stmt.close();

        stmt2.close();

        con.close();

        System.out.println("删除了resources_info_news 表" + i + "行记录");

    }

    public static void programDelete() throws ClassNotFoundException,

            SQLException {

        Class.forName("oracle.jdbc.driver.OracleDriver");

        String url = "jdbc:oracle:thin:@数据库IP:端口号:服务器名称";

        String user = "tvmer";

        String password = "tvmer";

        Connection con;

        con = DriverManager.getConnection(url, user, password);

        String str1 = "delete  from  resources_info_program   where  play_time>to_date('20111023 0010','yyyymmdd hh24mi')and play_time<to_date('20111023 0030','yyyymmdd hh24mi')";

        String str3 = "select path_info from  resources_info_program  where resourcs_type not in ('TS','SJPG','JPG','AVI','MJPG','MTG','XMLTMF','MP4','MP4_MAIN','PTMF')  and play_time>to_date('20111023 0010','yyyymmdd hh24mi')and play_time<to_date('20111023
0030','yyyymmdd hh24mi')";

        PreparedStatement stmt1 = con.prepareStatement(str1);

        ;

        PreparedStatement stmt2 = con.prepareStatement(str3);

        ResultSet rs1 = stmt2.executeQuery();

        String child = null;

        //File fi = new File("e:\\CustomerCache\\", child);

        //int j = stmt1.executeUpdate();

        while (rs1.next()) {

            child = rs1.getString("path_info");

            File fi = new File("e:\\CustomerCache\\", child);

            deletFiles(fi);

        }

        int j = stmt1.executeUpdate();

        rs1.close();

        stmt1.close();

        con.close();

        System.out.println("删除了resources_info_program 表" + j + "行记录");

    }

    public static void deletFiles(File f) {

        if (f.exists()) { // 判断文件是否存在

            if (f.isFile()) { // 判断是否是文件

                f.delete(); // delete()方法 是删除的意思;

            } else if (f.isDirectory()) { // 否则如果它是一个目录

                File files[] = f.listFiles(); // 声明目录下所有的文件 files[];

                for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件

                    deletFiles(files[i]); // 把每个文件 用这个方法进行迭代

                }

            }

            f.delete();

        } else {

            System.out.println("所删除的文件不存在!" + '\n');

            System.out.println("不存早的文件路径为" + f + '\n');

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息