java4all@1986 java. Powered by Blogger.

How to write read into CSV File?

>> Thursday, May 26, 2011

For example if we want create a csv file like
          Name   Place  Age
         bhas      VMR   23
         Aja        ONG   21





Here below code will explain easily how to to write the above data into CSV File
 
public class GenerateCsv1
{
    String name="bhaskar";
   

   private static void generateCsvFile(String sFileName)
   {
    try
    {
        FileWriter writer = new FileWriter(sFileName);

      
    writer.append("Name");
        writer.append(',');
writer.append("Place");
        writer.append(',');
        writer.append("Age");
        writer.append('\n');

        writer.append("Bhas");
        writer.append(',');
writer.append("Vmr");
        writer.append(',');
        writer.append("23");
            writer.append('\n');

        writer.append(""Aja);
        writer.append(',');
writer.append(""Ong);
        writer.append(',');
        writer.append("21");
        writer.append('\n');

        //generate whatever data you want

        writer.flush();
        writer.close();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }
    }
   public static void main(String [] args)
   { String csv="c://bhaskar.csv";
       GenerateCsv1.generateCsvFile(csv);
   }
}

      

Read more...

Diffrence between collection and Collections?

Collection:It is an interface for representing a group of objects as single entity.

 Collections:It is an Utility class available Java.util package for defining several utility methods for collection objects.

Read more...

Difference between Arrays and Collections?

Arrays:

  •  They are fixed in size.
  • With respect to memory arrays are not preferable
  • Arrays shows very good Performance
  • Can hold only Homogeneous data elements.
  • Arrays can hold both primitive data types and objects
  • They doesnot have any underlying datastructures.

Collections:
  • Growable in nature.
  • With respect to memory collections are recommended.
  • It shows poor Performance.
  • Can hold both homogeneous and Heterogeneous .
  • They have underlying datastructure.
  • Collections hold only Objects.

Read more...

How to itreate objects in Map?

Map: It can be used to store group of objects in the of name value pairs.

package com.centris.atr.bean;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class IterateMap {
     public static void main(String[] args) {
        Map m=new HashMap();
        m.put(1, "bhaskar");
        m.put(2,"Ajantha");
        m.put(3, "prathap");
        Set s=m.entrySet();
        Iterator it =s.iterator();
        while(it.hasNext())
        {
            System.out.println(it.next());
        }
    }

}

Read more...

What is meant by Struts Tokenizer?

Struts Tokenizer:To prevent duplicate action acting on request.

 Methods: 1.resetToken
                    2.isTokenValid
                    3.saveToken


       NOTE:We have to use the below code in html (or) our jsp page

              <input type="hidden">  name="<% org.apache.struts.taglib.html.Constants.Token_Keys%>" value="<%Globals.TRANSACTION_TOKEN_KEYS%>"

Read more...

Difference between Jdbc.close and DataSource.close?

  • In Jdbc con.close() terminates the connection to the D.B,it automatically closes the resultset and statement object.
  • But in case of Datasource con.close ,it just return the connection to the connection pooling ,it doesnot close the connection  for the D.B.It is our responsibility to close the reultsset and statement object associated with that connection.

Read more...

DataBase connection in Java Application? or How to get DataSource from TomCat?

The below steps will explain you how to get DataSource from Tomcat.

STEP1:) In Tomcat Folder. Tomcat-->Context.xml 6.0 server.xml 5.0
                   Open Context.XML file ,under context tag add Resource tag with following attributes.

<Resource
           name="jdbc/projectname"
           auth="Container"
           type="javax.sql.DataSource"
           maxActive="100"
           maxIdle=300"
           maxWait="1000"
           username="bhaskar"
           password="bhaskar"
           driverclassName="com.mysql.jdbc.Driver"
           url="jdbc.mysql://localhost:3306/schemanameoftable"  />


    STEP2:
 After configuring in Tomcat we have to get the connection in our application.For This go through belo0w program....


             import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


public class DbUtility {

    private static  Context objContext = null;
    private static  DataSource objDataSource = null;
   
     static
     {
         System.out.println("inside the static block of dbutility");
        try
        {
            objContext = new InitialContext();
             objDataSource =(DataSource)  objContext.lookup("java:/comp/env/jdbc/projectname");
             System.out.println("objDataSource is: "+objDataSource);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Error while in datasource;");
        }
       
       
       
     }
   
    public static Connection getConnection() throws Exception{
       
        Connection con = null;
       
        if(objDataSource == null)
        {
            System.out.println("error while getting the datasource");
        }
        else
        {
            con= objDataSource.getConnection();
        }
       
        return con;
    }
   
   
   
    public static void closeConnecton(Connection objConnection) throws Exception
    {
        System.out.println("inside closing the connection");
        if(objConnection != null)
        {
            objConnection.close();
        }
    }
}
Connection pooling means reusing the connection witout closing the connection.For this go for difference between Jdbc.close and DataSource.close.

Read more...

How we will provide Junit Test to our application?

  • If a user is testing the functionality which was written that we have called as "UnitTesting.".
  • If a programmer is testing  the code that was written other programmer it is called "Peertesting"
  • Generally we will use Junit  for UnitTesting.
            STEPS for Writting Junit:
           1.)Develop our main class
                  public class Calculate
                     {
                          public int AddNumber(int i,int j){
                            return i+j;
                      }

2.Develop a test case for this class
Note:Our test case class must extends Junit.framework.Tesrcase
 Testcase class of Junit framework is having 2 life cycle methods and some Testxxx() methods.
 Note:setup(), tearDown() are called life cycle methods of Junit.

setUp():It is used for instanciation of requried resources.It is like init() .
tearDown(): It is for releasing the resources .It is similar to destroy() of servlet.
In testxxx() method we will write our logic for testing the functionality. 
  • we will use fail(string s) methos to show & terminate the method.
  • We will write the assertxxx() method to test the logic.
  • We can also use the tail() in the catch().
  • We will write the assertxxx() methods in the try block.
                                                          
              Sample Program:
 public clas MultipleTestCase extend TestCase{
  public Calculate objcalculate=null;

protected void setUp() throws Exception(){
Super.setUp();
objcalculate=new Calculate();



protected void tearDown() throws Exception(){
Super.tearDown();
objcalculate=null;




 
public void test multiplicationTesting(int i,int j) {

int k=10;

int p=30;
if(k==0|| p==20)
{
tail("zero is not accepted")
}
assertEquals(100,objCalculate.multipleNumber(3,6))

System.println("After  satisfing the assert equal")
}
}

Read more...

Difference between Enum and Interface?

  • Interface doesnot contain any constructors
  • Enum act as a class and extend java.lang.Enum,but it cannot other class ,but implements any n.o of interfaces.
  • But an interface can extend any n.o Interfaces.
  • Java.lang.Enum is base class of all enum's preesent in java.It is an abstract class,it implements Serializable, comparable Interfaces

Read more...

What is meant by enum?

Enum:It is defined as group of named constants.

  •    A enum is a class ,it should be used to extend java.lang.Enum but not other classes.
  • If we are taking only constants it is not necessary to put semicolons; at end of constants.
  • It also contains constructors, methods.
  • All constants  are public static and final.
  • Enum is a child class of java.lang.Enum
  • So enum cannot extend another class.
  • Enu can implement any n.o of interfaces.
  • We can declare enum ,inside and outside of a class,but not inside the method.
  • In enum constants are compulsary

Read more...

JDK 1.5 Features

Features of JDK 1.5 are:

 1.)Genericis
2.)Var-args
3.)Metadata(annotations)
4.)AutoBoxing and UnBoxing
5.)Enum
6.)Enhanced For loop
7.Static Imports
8.)Scanners Class
9.)Concurrency utilities
10.)Queues.

Read more...

How to getFacebook share button,Like,Login,Twitter button?

First Step: Go to your blog Account .Click on Dashboard icon.

SecondStep: Click on the design icon,now you will get a new page like ARRANGE ELEMENTS,In this section click on EDIT\HTML icon.

Third Step: copy the below code and Paste in EDIT/HTML below the title text.

<!-- floating page sharers Start Share This With Friends-->
<style>
#pageshare {position:fixed; bottom:15%; left:10px; float:left; border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;background-color:#fff;padding:0 0 2px
0;z-index:10;}
#pageshare .sbutton {float:left;clear:both;margin:5px 5px 0 5px;}
.fb_share_count_top {width:48px !important;}
.fb_share_count_top, .fb_share_count_inner {-moz-border-radius:3px;-webkit-border-radius:3px;}
.FBConnectButton_Small, .FBConnectButton_RTL_Small {width:49px !important; -moz-border-radius:3px;-webkit-border-radius:3px;}
.FBConnectButton_Small .FBConnectButton_Text {padding:2px 2px 3px !important;-moz-border-radius:3px;-webkit-border-radius:3px;font-size:8px;}
</style>
<div id='pageshare' title="Share This With Your Friends">
<div class='sbutton' id='fb'>
<a name="fb_share" type="box_count" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share"
type="text/javascript"></script>
</div>
<div class='sbutton' id='rt'>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" >Tweet</a>
<script src='http://platform.twitter.com/widgets.js' type="text/javascript"></script>
</div>
<div class='sbutton' id='su'>
<script src="http://www.stumbleupon.com/hostedbadge.php?s=5"></script>
</div>
<div class='sbutton' id='digg' style='margin-left:3px;width:48px'>
<script src='http://widgets.digg.com/buttons.js' type='text/javascript'></script>
<a class="DiggThisButton DiggMedium"></a>
</div>
<div class='sbutton' id='gb'>
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<fb:like layout="box_count" show_faces="false" font=""></fb:like></div>
<div style="clear: both;font-size: 9px;text-align:center;"><a href="http://www.ogbongeblog.com/2011/04/how-to-add-facebook-share-like-buttons.html">Get this</a></div>
</div>
<!-- floating page sharers End -->

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP