Monday 14 March 2016

How to run a .sql file from java process specially if data is huge in the database and jdbc connections time out due to long running query




private  void extractDataInCsv() {

    try {
   
   
    File file = new File(this.getClass().getResource("/com/ibm/commerce/data/OrdersDataExtract.sql").toURI());

       
        Runtime rt = Runtime.getRuntime();
        String  executeSqlCommand= null;
       
        if(getRequestProperties().getBoolean("localDev",false)){
        executeSqlCommand = "db2cmd /c /i /w && db2 -tvf "+file.getAbsolutePath(); // local dev  windows
        }else{
        executeSqlCommand = "db2 -vf "+file.getAbsolutePath();  // linux
        }
       
        Process pr = rt.exec(executeSqlCommand);
       
        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
    System.out.println(line);
 }
//pause for 15se
Thread.sleep(15000);
       
     } catch (Exception e) {
     e.printStackTrace();
     }
}

Script in OrdersDataExtract.sql is as follows:


connect to dbname user dbusername using password
set schema db2inst1

-- extract data
EXPORT TO /opt/IBM/WebSphere/AppServer/temp/OrdersDataExtract.csv OF DEL MODIFIED BY NOCHARDEL striplzeros decplusblank select with ur
terminate


No comments:

Post a Comment

How to customize java.util.logging.Logger class to write logs in separate file than System.out.log in Websphere commerce/ HCL commerce)

/** * This method updated the passed in java.util.logging.Logger object with * custom file handler to write logs data form that class ...