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