public class TableBcpProcessor extends Thread

public class TableBcpProcessor extends Thread
{

    private static DebugPrn dMsg = new DebugPrn(TableBcpProcessor.class.getName());

    private DatabaseInfo dbInfo = null;
    private String dbName = null;
    private String[] tableNames = null;
    private String path = null;
    private String[] fileNames = null;

    //导出还是导入,0表示导入,1表示导出
    private int mode = 0;

    public final static int BCP_IN = 0;
    public final static int BCP_OUT = 1;

    AbstractProcessJFrame frame = null;
    /**
     *
     * @param frame
     * @param databaseInfo
     * @param dbName
     * @param tableNames 类似CAF_INFORM
     * @param path  类似"I:/12.5oracle导出的文件/20100625"
     * @param fileNames 类似 CAF_INFORM_20100630_1929.txt
     * @param mode
     */
    public TableBcpProcessor(AbstractProcessJFrame frame, DatabaseInfo databaseInfo, String dbName,
            String[] tableNames, String path, String[] fileNames, int mode)
    {
        this.frame = frame;
        this.dbInfo = databaseInfo;
        this.dbName = dbName;
        this.tableNames = tableNames;
        this.path = path;
        this.fileNames = fileNames;
        this.mode = mode;

    }

    /**
     *
     */
    public void run()
    {
        for(int i = 0; i < tableNames.length && i < fileNames.length; i++)
        {

            testBcp(tableNames[i], fileNames[i]);
        }

        frame.clearStatus();
        frame.guiChange(true);
        //MainFrame.closeWaitDialog();
    }

    public void testBcp(String tableName, String fileName)
    {
        String s = "";
        StringBuffer cmdBuffer = new StringBuffer();
        cmdBuffer.append("bcp ").append(dbName).append("..").append(tableName);
        if(mode == 0)
        {
            cmdBuffer.append(" in ");
            cmdBuffer.append(fileName);
        }
        else if(mode == 1)
        {
            cmdBuffer.append(" out ");
            cmdBuffer.append(path).append(System.getProperty("file.separator")).append(fileName);
        }
        else
        {
            return;
        }
        //对于导出的txt文件之所以没有分页,是因为bcp命令导入的时候,可以分批导入,这里每批次导入1000条
        cmdBuffer.append(" -n -b1000 -U");
        cmdBuffer.append(dbInfo.getUser()).append(" -P").append(dbInfo.getPassword()).append(" -S")
                .append(dbInfo.getDBSIp());

        Process process = null;
        BufferedReader br = null;
        try
        {
            //Runtime.getRuntime().exec("   bcp   /"   northwind.dbo.authors/"     out     /"d://chengxianke.txt/"   -c   -q   -U/"sa/"   -P/"123456/"   -S   chengxianke//cheng   ");  
            /*
             process = Runtime
             .getRuntime()
             .exec(
             "bcp uep_caf_fm..caf_alarmcode out D://alarm_code.txt  -n -b1000 -Usa -Pdata -S10.40.182.168");

             process = Runtime
             .getRuntime()
             .exec(
             "bcp uep_caf_fm..caf_alarmcode in D://alarm_code.txt  -n -b1000 -Usa -Pdata -S   ");*/

            process = Runtime.getRuntime().exec(cmdBuffer.toString());

//            dMsg.error(cmdBuffer.toString());
//            frame.appendStr(cmdBuffer.toString() + System.getProperty("line.separator"));

            br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while((line = br.readLine()) != null)
            {
                dMsg.error(line);

                frame.appendStr(line + System.getProperty("line.separator"));
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();

        }
        finally
        {

            if(br != null)
            {
                try
                {
                    br.close();
                }
                catch(IOException e)
                {
                    dMsg.error(e.toString());
                    //System.out.println(e.toString());
                }

            }
            dMsg.debug(s);
            //System.out.println(s);
            process.destroy();
            try
            {
                int exitVal = process.waitFor();
                dMsg.debug("exitVal:" + exitVal);
                //System.out.println();
            }
            catch(InterruptedException ee)
            {
                dMsg.error(ee.toString());
                //System.out.println(ee.toString());
            }
        }
        return;

    }
}

### ThreadDemo 类的实现或用法 在 Java 中,`Thread` 是多线程编程的核心部分之一。下面是一个关于 `ThreadDemo` 的简单实现及其用法的例子。 #### 创建和启动线程的方式 可以通过继承 `Thread` 或者实现 `Runnable` 接口来创建线程。以下是两种方式的具体示例: #### 方法一:通过继承 `Thread` ```java class MyThread extends Thread { @Override public void run() { System.out.println("Thread is running..."); } } public class ThreadDemo { public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); // 启动线程并调用run方法 } } ``` 上述代码展示了如何通过继承 `Thread` 来定义一个新的线程类,并重写其 `run()` 方法[^1]。 #### 方法二:通过实现 `Runnable` 接口 ```java class Task implements Runnable { @Override public void run() { System.out.println("Task is being executed by a thread..."); } } public class ThreadDemo { public static void main(String[] args) { Task task = new Task(); Thread thread = new Thread(task); thread.start(); // 启动线程并执行task.run() } } ``` 这种方式更加灵活,因为它不会占用继承的机会(Java 不支持多重继承),适合更复杂的场景[^2]。 #### 使用 `Future` 和 `Callable` 进行异步操作 当需要获取线程执行的结果或者控制线程的状态时,可以使用 `Future` 和 `Callable` 结合 `ExecutorService` 实现更为高级的功能。例如,取消任务的操作可以用到 `cancel` 方法[^3]。 ```java import java.util.concurrent.*; class TaskWithResult implements Callable<String> { @Override public String call() throws Exception { return "This result comes from the background task."; } } public class ThreadDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit(new TaskWithResult()); try { if (!future.isDone()) { System.out.println(future.get()); // 获取任务结果 } } catch (CancellationException e) { System.out.println("Task was cancelled."); } boolean mayInterruptIfRunning = true; future.cancel(mayInterruptIfRunning); // 尝试取消未完成的任务 executor.shutdown(); } } ``` 以上代码片段展示了一个简单的异步任务提交机制以及如何安全地取消尚未完成的任务。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值