WEB开发网
开发学院数据库DB2 在 DB2 for Linux, UNIX and Windows 中使用管道加... 阅读

在 DB2 for Linux, UNIX and Windows 中使用管道加载数据

 2010-04-27 00:00:00 来源:WEB开发网   
核心提示: 在 Windows 中编译 C 程序要编译和创建动态链接库 (DLL),您需要使用 Windows cl.exe 编译器,在 DB2 for Linux, UNIX and Windows 中使用管道加载数据(5),如果还没有 cl.exe,请下载 Microsoft Visual Studio

在 Windows 中编译 C 程序

要编译和创建动态链接库 (DLL),您需要使用 Windows cl.exe 编译器。如果还没有 cl.exe,请下载 Microsoft Visual Studio Express Edition 获取它。有关如何使用 Visual Studio 来编译 C 程序的详细步骤并不在本文的讨论范围之内。但是,您可以在命令行中使用 cl.exe 来创建 DLL,如下所示:

cl -I"C:\Program Files\IBM\Java50\include" -I"C:\Program Files\IBM\Java50\include\win32" 
    -LD Pipe.c -FePipe.dll    

使用 cl.exe 创建 DLL 时,记住将 ibm_Pipes.h 文件复制到当前目录,并将到 Java include 目录的引用替换为系统上该目录的实际位置。

使用 Windows 管道的示例 Java 程序

使用清单 3 中的代码在 Windows 上创建一个指定管道(使用在 Pipes.h 头文件中声明并在 Pipe.c 代码中实现的本机方法。)

清单 3. 使用 Windows 管道的示例 TestPipe.java 程序

package ibm; 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
 
public class TestPipe 
{ 
  static final int ERROR_PIPE_CONNECTED = 535; 
  static final int ERROR_BROKEN_PIPE = 109; 
  private int namedPipeHandle; 
  private String pipeName, srcFile; 
  private int pipeBuffer = 131072, fileBuffer = 8192; 
  
  public TestPipe(String pipeName, String srcFile) 
  { 
   this.pipeName = pipeName; 
   this.srcFile = srcFile;   
  } 
  
  private void log(String message) 
  { 
   System.out.println(message);   
  }  
 
  private boolean createPipe() 
  { 
   boolean ok = false; 
   namedPipeHandle = Pipes.CreateNamedPipe(pipeName, 
        0x00000003, 0x00000000, 2, pipeBuffer, 
        pipeBuffer, 0xffffffff, 0); 
   if (namedPipeHandle == -1) 
   { 
     log("CreateNamedPipe failed for " + pipeName + 
        " for error " + " Message " + 
          Pipes.FormatMessage(Pipes.GetLastError())); 
     ok = false; 
   } else 
   { 
     log("Named Pipe " + pipeName + 
       " created successfully Handle=" + namedPipeHandle); 
     ok = true; 
   } 
   return ok; 
  } 
  
  private boolean connectToPipe() 
  { 
   log("Waiting for a client to connect to pipe " + pipeName); 
   boolean connected = Pipes.ConnectNamedPipe(namedPipeHandle, 0); 
   if (!connected) 
   { 
     int lastError = Pipes.GetLastError(); 
     if (lastError == ERROR_PIPE_CONNECTED) 
      connected = true; 
   } 
   if (connected) 
   { 
     log("Connected to the pipe " + pipeName); 
   } else 
   { 
     log("Falied to connect to the pipe " + pipeName); 
   } 
   return connected; 
  } 
 
  public void runPipe() 
  { 
   if (createPipe()) 
   { 
     if (!connectToPipe()) 
     { 
      log("Connect ConnectNamedPipe failed : " + 
         Pipes.FormatMessage(Pipes.GetLastError())); 
      return; 
     } else 
     { 
      log("Client connected."); 
     } 
     
     try 
     { 
      File f1 = new File(this.srcFile); 
      InputStream in = new FileInputStream(f1); 
      log("Sending data to the pipe"); 
      byte[] buf = new byte[fileBuffer]; 
      int len, bytesWritten; 
      while ((len = in.read(buf)) > 0) 
      { 
       bytesWritten = Pipes.WriteFile(namedPipeHandle, buf, len); 
       log("Sent " + len + "/" + bytesWritten + 
                " bytes to the pipe"); 
       if (bytesWritten == -1) 
       { 
         int errorNumber = Pipes.GetLastError(); 
         log("Error Writing to pipe " + 
              Pipes.FormatMessage(errorNumber)); 
       }          
      } 
      in.close(); 
      Pipes.FlushFileBuffers(namedPipeHandle); 
      Pipes.CloseHandle(namedPipeHandle); 
      Pipes.DisconnectNamedPipe(namedPipeHandle); 
      log("Writing to the pipe completed."); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
   }   
  } 
  
  public static void main(String[] args) throws IOException, 
        InterruptedException 
  { 
   String pipeName = "\\\\.\\pipe\\mynamedpipe"; 
   String fileName = "C:\\db2tabledata.txt";; 
   TestPipe testPipe = new TestPipe(pipeName, fileName); 
   testPipe.runPipe(); 
  } 
} 

上一页  1 2 3 4 5 6 7 8  下一页

Tags:DB for Linux

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接