Monday, June 29, 2015

Write a data into Excel file in java.

Writing the data into java is having few easy steps.

Step 1- Create HSSFWorkbook class object.
HSSFWorkbook workbook = new HSSFWorkbook();

Step 2- Call workbook.createSheet() method by mentioning the sheet name.
HSSFSheet sheet = workbook.createSheet("Sample sheet");

Read Data from Excel file in java.

We cas read or write the data in Excel using apache poi apis. In below example I'm going to use "poi-3.12-20150511.jar".

Step 1- Create Excel file with some data.

Year    Country    By
1975    West Indies    17 run
1979    West Indies    92 run
1983    India    43 run
1987    Australia    7 run
1992    Pakistan    22 run
1996    Sri Lanka    7 wicket
1999    Australia    8 wicket
2003    Australia    125 run
2007    Australia    53 run
2001    India    6 wicket
2015    Australia    7 wicket

Step 2- Create
String fileName = "D:\\Ashish\\Personal\\WebApplication1\\iccworlcuplist.xls";
FileInputStream file = new FileInputStream(new File(fileName));

Read the data from CSV file and Map it to java object in java.

If you want to read the CSV file and store the data into java object no need to set the data into java object one by one. Opencsv has given a nice feature for this.

Step 1- Create CSV file."worldcup.csv"

1975,WestInddies,Australia,17 Run
1979,WestInddies,England,92 Run
1983,India,WestInddies,43 Run
1987,Australia,England,7 Run
1992,Pakistan,England,22 Run
1996,Sri Lanka,Australia,7 Wicket
1999,Australia,Pakistan,8 Wicket
2003,Australia,India,125 Run
2007,Australia,Sri Lanka,53 Run
2011,India,Sri Lanka,6 Wicket
2015,Australia,New Zeland,7 Wicket

Write Data into csv file in java.

To reading the data from csv file either use java IO library or usr third pary jar like "opencsv.jar". In given example I'm using opencsv.jar.

Step 1-  Create CSVWriter object.
String csv = "D:\\Ashish\\Personal\\WebApplication1\\worldcupwrite2.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));

Step 2- Create String array for data
String[] country = "WestInddies#India#Australia#Pakistan#Sri Lanka".split("#");

Step 3- Call writer.writeNext(country) method and pass the String array object as data.

Read Data at a time from csv file in java.

To reading the data from csv file either use java IO library or usr third pary jar like "opencsv.jar". In given example I'm using opencsv.jar.

Step 1- Create csv file."worldcup.csv"

1975,WestInddies,Australia,17 Run
1979,WestInddies,England,92 Run
1983,India,WestInddies,43 Run
1987,Australia,England,7 Run
1992,Pakistan,England,22 Run
1996,Sri Lanka,Australia,7 Wicket
1999,Australia,Pakistan,8 Wicket
2003,Australia,India,125 Run
2007,Australia,Sri Lanka,53 Run
2011,India,Sri Lanka,6 Wicket
2015,Australia,New Zeland,7 Wicket

Read Data from csv file line by line in java.

To reading the data from csv file either use java IO library or usr third pary jar like "opencsv.jar". In given example I'm using opencsv.jar.

Step 1- Create csv file. "worldcup.csv"

1975,WestInddies,Australia,17 Run
1979,WestInddies,England,92 Run
1983,India,WestInddies,43 Run
1987,Australia,England,7 Run
1992,Pakistan,England,22 Run
1996,Sri Lanka,Australia,7 Wicket
1999,Australia,Pakistan,8 Wicket
2003,Australia,India,125 Run
2007,Australia,Sri Lanka,53 Run
2011,India,Sri Lanka,6 Wicket
2015,Australia,New Zeland,7 Wicket

Monday, May 25, 2015

Pagination in java jsp.

The pagination is very useful when we have large number of records in a table. If we displaying all the record the at a time it will take more time to load all the data and taking more memory. So it's not a good for either memory or performance point of view. If we go for pagination it will load limited amount of record at a time so it will good for both memory and performance point of view.

In this example i'm going to display one of db table record in tabular format using pagination. I'm loading only 5 record at a time. Each request fetching  only 5 Record at a time by using below variable-
showRows = 5 (display the number of record per page)
totalRecords = 5 display the number of page at a time like 1,2,3,4,5 hyper link will come on each time

Monday, May 11, 2015

How to write a XML file in java.

Step 1- Create document

Step 2- Create root element

Step 3- Append root into document

Step 4- Create child element of root element

How to read XML file in java.

We can read a xml by following stpes-

Step 1- Create File object of xml file.

1
fXmlFile = new File("src\\myXml.xml");

Step 2- Create a document and parse the xml and sotore it into document object

1
2
3
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

How to transfer ResultSet object over network in java.

As we know the ResultSet object can not be transfer over the network. So for transferring the ResultSet object we need to set this object in Custom object and send this custom object over the network.

Step 1- Create UserTest class

Step 2- Retrieve the ResultSet object and set it into custom object (UserTest)

Saturday, May 9, 2015

How to add two array in java.

Step 1- Create two array

1
2
String a0[] = { "A", "E", "I" };
String b0[] = { "O", "U" };

Step 2- Convert these arrays into List

1
2
List a1 = new ArrayList(Arrays.asList(a0));
List b1 = new ArrayList(Arrays.asList(b0));

How to get the column name of table dynamically in jdbc.

While displaying the table data in jdbc we have to get ResultSet object using ResultSet we are displaying the table data by using rs.getInt(1) or rs.getString(“name”) like this.

But, If we don’t know the column name or number of column in table  we can use ResultSetMetaData for this. Using ResultSetMetaData we can  get the number of column, type of column, name of column and so on.

How to sort the object based on Id and Name in Java

If we have the List, Map or any other collection object we sort by using calling Collections.sort() method directly. But if we have our custom object we have to do some extra to sort our custom object. Assume that we have one Person Object like below

class Person{
    int id;
    String name;

    Setter…
    Getter…
}

Serialize and Deserialize the object in java.

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization.

In this process there are three steps
1-    Create java class by implementing the Serializable class.
2-    Create SerializeDemo class to write a java object into file.
3-    Create DeserializeDemo class to read the java object from file.

How to download a file from server in java

To download a file from server is very easy task just get the file name and path and set the file name into response header (Content-Disposition) .
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

How to upload a file at server and store path in database in java.

To upload a file at server and storing a path in database we have to use JavaJoom Api. Add "uploadbean.jar" in your application and follow below steps –

How to send SMS in java.

To send a sms in java is very similar to send email in java. At the place of mail server (smtp host) we need to give some third party (which are providing the sms service) smtp host.
Many number of SMS provider in the market we can choose any of them. Here in my below 2 example I have choose two sms provider

1-    ipipi.com
2-    vianett.com

First example is using ipipi.com and second one is vianett.com.

Friday, May 1, 2015

How to send the mail in Java.

Sending mail in java is very easy just follow the below stpes-


Step 1- Create a property with smtp host, smtp port. Here I’m going the send the mail using gmail so I’m using smtp host as smtp.gmail.com and port is 465.

How to send the attachment mail in java

Sending mail in java is very easy just follow the below stpes-


Monday, April 27, 2015

How to convert Java Object in JSON and how to retrieve it at client side

To converting a java object in JSON we need to user some 3rd party API here I’m using “gson.2.2.4.jar” .


Saturday, April 4, 2015

How to remove duplicate element from String array and List.

1 ) String Array.
Ex : String nameArray = {“Ram”,”Shyam”,”Mohan”,”Sohan”,”Ram”,”Shyam”,”Mohan”};

2 ) ArraList.
Ex : [“Ram”,”Shyam”,”Mohan”,”Sohan”,”Ram”,”Shyam”,”Mohan”]

As we know the Set collection having the feature to allow the store unique value. Based on that we have follow two step here to removing the duplicate from string array-

Sunday, March 29, 2015

How to create Bar Code in java (using aspose and itext) ?

Using aspose API.

For creating a Bar Code is very simple in java using aspose apis. We can use “aspose-barcode-5.4.2-jdk15.jar” for this.


Thursday, March 26, 2015

How to create a Captcha in java?

Creating a captach in java is nothing but converting some random number or character in Image form in Servlet and send it to back as response in form of image to browser.

testCaptcha.jsp



Monday, March 23, 2015

How to connect DB2 database in java?

We have to follow 6 steps to play with DB2 database in java jdbc.

Note: We have use “db2jcc_license_cu-version.jar” and “db2jcc-version.jar” ex- “db2jcc_license_cu-1.0.jar” and “db2jcc-1.0.jar”

Step 1:  Create table in database

CREATE TABLE USER_DETAILS (
USER_ID INTEGER,
USER_NAME VARCHAR(50) NOT NULL,
PASSWORD VARCHAR(50) NOT NULL,
PRIMARY KEY(USER_ID)
)

How to connect Oracle database in java?

We have to follow 6 steps to play with Oracle database in java jdbc.

Note: We have use “ojdbc-version.jar” ex- ojdbc14.jar

Step 1:  Create table in database
CREATE TABLE USER_DETAILS (
            USER_ID NUMBER(5) NOT NULL,
            USER_NAME VARCHAR2(50) NOT NULL,
            PASSWORD VARCHAR2(50) NOT NULL,
) ;

How to connect MySql database in java ?

We have to follow 6 steps to play with MySql database in java jdbc.

Note: We have use “mysql-connector-java-version-bin.jar” ex- mysql-connector-java-5.0.3-bin.jar

Step 1:  Create table in database
CREATE TABLE ‘USER_DETAILS’ (
            ‘USER_ID’ INT(5) NOT NULL,
            ‘USER_NAME’ VARCHAR(50) NOT NULL,
            ‘PASSWORD’ VARCHAR(50) NOT NULL,
            PRIMARY KEY(‘USER_ID’)
) ;

How to create Singleton class in java?

Step 1: Create Class “SingleObject.java”
Step 2: Create a private constructor so no one can instantiate  this class.
Step 3: Create the static method getInstance and this will return the object of SingleObject class.
Step 4: Create client program and call this static method of SingleObject class this method will return the same object each time.
Step 5: We can verify the the singleton behavior by hashCode. The hasCode is same for both object.