Saturday, September 14, 2013

How to get table columns in sql server

This query return columns of table in SQL Server database 

 DECLARE @TABLE_NAME VARCHAR(MAX)  
 SET @TABLE_NAME='customer_master'  
 SELECT c.name  
 FROM sys.tables AS t, sys.columns AS c  
 WHERE t.object_id= c.object_id  
 AND t.name= @TABLE_NAME  

Friday, May 31, 2013

Using java read Excel(.xls) file

In this example demonstrate how to read Excel(.xls) file in java using apache poi jar file is need for dependency.


  // Import class   
  import java.io.File;   
  import java.io.IOException;  
  import jxl.Cell; //class from apache POI  
  import jxl.CellType;  
  import jxl.Sheet;  
  import jxl.Workbook;  
  import jxl.read.biff.BiffException;  
  public class Test {  
  private String inputFile;  
  public void setInputFile(String inputFile) {  
   this.inputFile = inputFile;  
  }  
  public void read() throws IOException {  
   File inputWorkbook = new File(inputFile);  
   Workbook w;  
   try {  
   w = Workbook.getWorkbook(inputWorkbook);  
   // Get the first sheet  
   Sheet sheet = w.getSheet(0);  
   // Loop over first 10 column and lines  
   for (int j = 0; j < sheet.getColumns(); j++) {  
    for (int i = 0; i < sheet.getRows(); i++) {  
    Cell cell = sheet.getCell(j, i);  
    CellType type = cell.getType();  
    // It return cell if type is lable   
       if (type == CellType.LABEL) {  
     System.out.println("I got a label "  
      + cell.getContents());  
    }  
    // It return cell if type is number   
    if (type == CellType.NUMBER) {  
     System.out.println("I got a number "  
      + cell.getContents());  
    }  
    }  
   }  
   } catch (BiffException e) {  
   e.printStackTrace();  
   }  
  }  
  public static void main(String[] args) throws IOException {  
   Test test = new Test();  
   test.setInputFile("C:\\Users\\cuboidology8\\Desktop\\New folder\\temp.xls");  
   //Set file location for reading   
   test.read();  
  }  
  }   

Tuesday, May 28, 2013

Delimiter separated text file using java.

Example to demonstrate, How to read text file data with delimiter separated in java

  import java.io.BufferedReader;    
  import java.io.FileReader;    
  public class Delimeter {    
   public static void main(String args[]) throws Exception {    
   /**    
   * Source file to read data from.    
   */    
   String dataFileName = "D://delimeter.txt";    
   /**    
   * Creating a buffered reader to read the file    
   */    
   BufferedReader bReader = new BufferedReader(    
    new FileReader(dataFileName));    
   String line;    
   /**    
   * Looping the read block until all lines in the file are read.    
   */    
   while ((line = bReader.readLine()) != null) {    
    /**    
    * Splitting the content of comma separated line    
    */    
    String datavalue[] = line.split(","); // here you can specify you delimeter form spliting    
    String value1 = datavalue[0];    
    String value2 = datavalue[1];    
    int value3 = Integer.parseInt(datavalue[2]);    
    double value4 = Double.parseDouble(datavalue[3]);    
    /**    
    * Printing the value read from file to the console    
    */    
    System.out.println(value1 + "\t" + value2 + "\t" + value3 + "\t"    
     + value4);    
   }    
   bReader.close();    
   }    
  }    

Read csv file using java

In This example I have to demonstrate, How to read comma separated text file in java 






  import java.io.BufferedReader;   

  import java.io.FileReader;   

  import java.io.IOException;   

  public class Comma {   

      public static void main(String args[]){   
           try {
// for csv file just change file name 
                BufferedReader in = new BufferedReader(   
                          new FileReader("D:\\uttam\\files\\comma.txt"));   
                String str;   
                str = in.readLine();   
                //System.out.println(str);   
                String[] ar =str.split(",");   
                for(int i=0;i<ar.length;i++){   
                     System.out.print(ar[i]+"\t");   
                }   
                while ((str = in.readLine()) != null) {   
                     ar=str.split(",");   
                     System.out.println();   
                     for(int i=0;i<ar.length;i++){   
                               System.out.print(ar[i]+"\t");   
                     }   
                }   
                in.close();   
           } catch (IOException e) {   
           System.out.println("File Read Error");   
           }  
      }            
 }   

Saturday, May 25, 2013

How to upload file on ftp using Apache Camel

 //This example is represent how to upload file to FTP using Apache Camel
 
 import org.apache.camel.builder.RouteBuilder;  
 import org.apache.camel.main.Main;  
 public class Myownclass {  
      public static void main(String[] args) {  
           try {  
                Main main = new Main();  
                main.addRouteBuilder(new RouteBuilder() {  
                     @Override  
                     public void configure() throws Exception {  
                          // TODO Auto-generated method stub  
                          errorHandler(deadLetterChannel("mock:error"));  
                          from("file:C:/Users/cuboidology8/Downloads?noop=true").to("ftp://cuboidology8@127.1.2.3:21/src?password=cuboidology8")  
                                    .log("Downloaded file ${file:name} complete.");  
                     }  
                });  
                main.enableHangupSupport();  
                main.run();  
                // create the camel context:  
           } catch (Exception e) {  
                // this is an example -> don't handle exceptions:  
                e.printStackTrace();  
           }  
      }  
 }  

Friday, May 24, 2013

how to copy file in java from source to destination directory(filecopier)

This example is demonstrate how to copy file in java from source to destination directory.

 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.io.OutputStream;  
 public class FileCopier {  
      public static void main(String args[]) throws Exception {  
           File inboxDirectory = new File("D:/uttam/app/eclipse/Test class/src/data/inbox");  
           File outboxDirectory = new File("D:/uttam/app/eclipse/Test class/src/data/outbox");  
           outboxDirectory.mkdir();  
           File[] files = inboxDirectory.listFiles();  
           System.out.println(files);  
           try{for (File source : files) {  
                if (source.isFile()) {  
                     File dest = new File(outboxDirectory.getPath() + File.separator  
                               + source.getName());  
                     copyFile(source, dest);  
                }  
           }}  
           catch(Exception e){}  
      }  
      private static void copyFile(File source, File dest) throws IOException {  
           OutputStream out = new FileOutputStream(dest);  
           byte[] buffer = new byte[(int) source.length()];  
           FileInputStream in = new FileInputStream(source);  
           in.read(buffer);  
           try {  
                out.write(buffer);  
           } finally {  
                out.close();  
                in.close();  
           }  
      }  
 }