Friday, January 31, 2014

Implementation of Restful Webservice with image

In Java EE 6, JAX-RS provides the functionality for Representational State Transfer (RESTful) web services. REST is well suited for basic, ad hoc integration scenarios. RESTful web services, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions. Before proceeding please read this article, Developing RESTful Web Services with JAX-RS.

Also please have look at the tutorial about "JAXB marshalling and unmarshalling", as it would be helpful to understand Restful Webservice.

For running this example, below mentioned particulars are used.
  • Tomcat 7.0.11
  • JDK 1.5
  • MyEclipse 8.6.1
1. Create a new Web Service Project, File -> New -> Web Service Project

Wednesday, January 29, 2014

Java program to download file from url

Below is the java program to download a file from url.

Java code to send meeting request using iCal4j

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter propose another meeting date/time.

iCalendar is used and supported by a large number of products, including Google Calendar, Apple iCal,IBM Lotus Notes,Yahoo Calendar, Microsoft Outlook etc

iCal4j is an API which is used to modifying existing iCalendar data or creating new iCalendar data. To know more about iCal4j, click here.

About the example 

This example is straight forward, we will create a calendar (net.fortuna.ical4j.model.Calendar) object and add a Meeting event to it. You can set various properties to the event like subject, location, description etc. Obviously you can customize it according to your requirement. This example will generate a .ics file (TestCalendar.ics). You can send this generated file via email to interested parties. Please see the below self explanatory java program.

For running below example you will need to add below mentioned JAR files in your classpath.
  • ical4j-1.0.3.jar
  • backport-util-concurrent-3.1.jar
  • commons-logging-1.1.1.jar
  • commons-lang-2.6.jar
To download the above JAR file, click here.

Java program to send email with attachments

Note: To run below example u need to add JavaMail jar files in your classpath

Java program to read csv file

Below is the java program to read a csv file.

Lets assume we have a below csv file.


"India","Delhi","Pranab Mukherjee"
"United Kingdom","London","David Cameron"
"China","Beijing","Hu Jintao"
"United States","Washington","Barack Obama"


"Country" "Capital" "President"
"India" "Delhi" "Pranab Mukherjee"
"United Kingdom" "London" "David Cameron"
"China" "Beijing" "Hu Jintao"
"United States" "Washington" "Barack Obama" 

Tuesday, January 28, 2014

Java program to reverse a string using array


Original string : Hello World

Reversed string : dlroW olleH

Java program to reverse a string without using string function

Below is the java program to reverse a string using recursive algorithm.

Java program to reverse a string word by word

Output :

Original String : How are you

Words reversed : you are How

Java program to reverse a string using recursion

Below is the java program to reverse a string using recursive algorithm.

Java program to find prime numbers

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself i.e it is divisible by one and itself. As of January 2014, the largest known prime number is 257,885,161 -1, a number with 17,425,170 digits. Below is the simple java program which will print the prime numbers upto the limit provided.

Monday, January 27, 2014

Java program to create an Icons of A-Z



Java program to print a-z, A-Z sequentially


public class PrintAlphabets {

public static void main(String[] args) {

for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
System.out.print(alphabet + " ");


for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
System.out.print(alphabet + " ");



a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 

Java program to create an image using Graphics2D

Please see the self explanatory java code below

Eclipse : Caused by: java.lang.ClassNotFoundException / Exception in thread "main"

java.lang.NoClassDefFoundError: com/javaxp/test
Caused by: java.lang.ClassNotFoundException: com.javaxp.Test
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.ClassLoader.loadClassInternal(
Exception in thread "main" 

There could be a couple of reasons for the above error

1. .class files not created properly

Project -> Clean -> Clean all projects / Clean project selected below -> OK

2. JVM is not able to pick the classpath

right click on the project -> Properties -> Java Compiler -> unclick the first box saying 'Enable project specific settings' -> Apply -> OK

3. A missing JAR file in the classpath

right click on the project -> Properties -> Java Build Path -> Libraries -> (Remove unwanted missing jar files if any) -> OK

Hope this helps in most of the cases.

Wednesday, January 22, 2014

DB2 Equivalant for Rownum of Oracle - ROW_NUMBER() OVER()

Syntax - Select ROW_NUMBER() OVER(), column_name1 from table

SQL0803N / SQLSTATE=23505

Error during Execute
 23505(-803)[IBM][CLI Driver][DB2] SQL0803N  One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "" constrains table "" from having duplicate values for the index key.  SQLSTATE=23505

Probably you are trying to insert a value in primary key / unique key which is already present and thus violating primary key, unique key constraints. Please make sure you are not entering duplicate values in primary key / unique key.

Tuesday, January 21, 2014

A parent row cannot be deleted because the relationship "R_1" restricts the deletion

Error during Execute
 23504(-532)[IBM][CLI Driver][DB2] SQL0532N  A parent row cannot be deleted because the relationship "R_1" restricts the deletion.  SQLSTATE=23504

You are trying to delete a specified row in the parent table which has dependencies on child table(s). First delete the record in the child table(s) and then try to delete the record in the parent table.