Tuesday 25 March 2014

Write data to Excel using Apache POI -Java

HI

This post is regarding writing data to a xlsx sheet.In java we had many powerful API's to write data to CSV or xlsx or xls files.One such API is apache POI by apache foundation.This API has classes and interfaces which helps us to write data to .csv or .xlsx sheets.

Here I am providing you a sample code snippet which helps you to explore and experiment more on this

ENVIRONMENT: java 1.6 sdk,apche poi  jars

Download location : http://poi.apache.org/download.html

Setup your environment add jars to the build path in eclipse.

Code begins:


 import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class sample {

 public static void main(String[] args) {
   try {
  String sample="test content";
  Workbook wb=new XSSFWorkbook();
  Sheet sheet=wb.createSheet("samplesheet");
  Row row=sheet.createRow(0);
  Cell cell=row.createCell(0);
  row.getCell(0).setCellValue(sample);
  FileOutputStream fop=new FileOutputStream("sample.xlsx",true);
  wb.write(fop);
  fop.flush();
  fop.close();
  } catch (Exception e) {
  
   e.printStackTrace();
  }

 }

}   

Output: This sample created a Excel sheet with first cell filled with the string test content

Friday 21 March 2014

Grails with Mysql database DataSource.groovy configuration

Hello viewers

In this post i am going to give a sample of code snippet which helps to configure Grails with mysql database. First of all grails is a framework which is developed by integrating hibernate and spring framework by springsouce .In a grails-app to communicate with database we provide all our configuration in DataSource.groovy file present under grails-app/conf folder in a grails project


dataSource {
    pooled = true
    dbCreate = "update"
    url = "jdbc:mysql://localhost:3306/yourDb"
    driverClassName = "com.mysql.jdbc.Driver"
    username = "root"
    password = "xxxx"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
    development {
        dataSource {
            pooled = true
            dbCreate = "update"
            url = "jdbc:mysql://localhost:3306/yourDb"
            driverClassName = "com.mysql.jdbc.Driver"
            username = "root"
            password = "xxxx"
        }
    }

and in grails we had plugins which will handle many operations.here to connect to mysql we need connector jar which in java we download and include it in our build path.But in grails right click on project in any of your IDE (Here i am using intellij idea) and in options choose grails-->plugins and search for mysql .Now install all mysql dependent plugins

then it get reflected in application.properties in your project as
plugins.mysql-connectorj=5.1.22.1

Here ends the configuration now you access mysql with your grails app


Thanks and regards
venkata naveen

Wednesday 5 March 2014

Run a Java Thread For Certain period of time java

A sample program on threads to demonstrate how to start a thread and run it for a specified period of time (say 2 minutes in this below example).

Code begins here.........


import java.util.Calendar;
class ThreatRunner extends Thread{
 @Override
 public void run() {
  System.out.println(" in the thread");
  try {
   sleep(10000);
  } catch (InterruptedException e) {

   e.printStackTrace();
  }
 }
}
public class ThreadWithTimer {
 public static void main(String[] args) {
  ThreatRunner tr=new ThreatRunner();
  Thread tt=new Thread(tr);
  Calendar mycal=Calendar.getInstance();
  long currentTimer=System.currentTimeMillis();
  System.out.println(mycal.getTime());
  while ((System.currentTimeMillis()-currentTimer)< 2*60*1000){
   tt.run();

  }
  System.out.println(mycal.getTime());

 }

}
Output: Thu Mar 06 12:17:04 IST 2014
in the thread
in the thread
Thu Mar 06 12:19:04 IST 2014