If you have DB2 scripts that you want to run through your Java programs, you normally use Runtime.getRuntime().exec method to run system commands. The following sample code tells how to run DB2 scripts.

The following code shows it for both Windows and Unix systems. The trick here is to run db2cmd with /c /i /w switches to make it working properly. For Unix systems, it assumes that you are running this program from db2 enabled shell or you have sourced your db2profile. Same way, you use ksh shell with -c switch and pass all arguments through a array variable.

Through this program, you are passing full path name of the db2 script. We also change directory to the parent of the script so that all other dependent scripts residing in the directory do not fail. This is not so much about Java programming but how to use shell capability to run multiple commands.

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RunDB2Script implements Runnable
{
   private static final SimpleDateFormat timestampFormat = 
      new SimpleDateFormat("yyyy-MM-dd HH.mm.ss.SSS");
   private static String osType = (System.getProperty("os.name").toUpperCase().
         startsWith("WIN")) ? "WIN" : (System.getProperty("os.name").toUpperCase()
               .startsWith("Z/OS")) ? "z/OS" : "OTHER";  
   private String db2ScriptName = "";

   public RunDB2Script(String db2ScriptName)
   {
      this.db2ScriptName = db2ScriptName;
   }
   
   public void run()
   {
      String line = null;
      Process p = null;
      BufferedReader stdInput = null, stdError = null;

      try
      {
         File f = new File(db2ScriptName);
         String dirName = f.getParent();
         if (osType.equalsIgnoreCase("win"))
         {
            p = Runtime.getRuntime().exec("db2cmd /c /i /w " + "cd " + dirName + 
                  " && db2 -tvf " + db2ScriptName);
            stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = stdInput.readLine()) != null)
            {
               if (!(line.equals("")))
                 log(line);
            }
            while ((line = stdError.readLine()) != null)
            {
               if (!(line.equals("")))
                  log(line);
            }
         } else
         {
            String cmd[] = {"/bin/ksh","-c", "cd " + dirName + 
                  " ; db2 -tvf " + db2ScriptName};
            p = Runtime.getRuntime().exec(cmd);
            stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = stdInput.readLine()) != null)
            {
               log(line);
            }
            while ((line = stdError.readLine()) != null)
            {
               if (!(line.equals("")))
                  log(line);
            }
         }
         stdInput.close();
         stdError.close();
         p.getInputStream().close();
         p.getErrorStream().close();
      } catch (Exception e) 
      {  
         e.printStackTrace();
      }
    }
   
    private static void log(String msg)
    {
      if (osType.equals("z/OS"))
      {
            System.out.println(timestampFormat.format(new Date()) + ":" + msg);        
      } else 
      {
            System.out.println("[" + timestampFormat.format(new Date()) + "] " + msg);          
      }
    } 
}