public class TableBcpProcessor extends Thread

本文介绍了一个使用Bcp命令进行数据库导入和导出的处理器类,详细解释了其构造函数、运行方法以及如何通过参数配置进行不同操作。

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;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值