Monday, 29 February 2016

Calling RestApi Web Services From Java Program

A JAVA Program to call RestApi Services :

package RestApiClientTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class NetClientGet {
public static void main(String[] args) {
 try {
 URL url = new URL("http:/localhost:8983/rest/calculatesum"); // rest api url
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
 } catch (MalformedURLException e) {
e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
 }
}

You need to include below jars in your class path:
1. jackson-databind
2. jackson-annotations
3. jackson-core

Maven details for the jars :
  <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1.1</version>
</dependency>

Thursday, 11 February 2016

Date Formatting in Java Example

Date Formatting in Java Example :
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatting {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date date=new Date();
        String str_date=df.format(date);
        System.out.println("dd-MM-yyyy: "+str_date);
        df.applyPattern("yyyy-MM-dd");
        System.out.println("yyyy-MM-dd: "+df.format(date));      
     
        System.out.println("*****************");        
        SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
        String str="10-02-2016";
        //Date date=new Date();
        Date date1=null;
try {
date1 = df2.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        df2.applyPattern("yyyy-MM-dd");
        System.out.println("yyyy-MM-dd: "+df2.format(date1));
}

}



String to Date :
private Date convertStringToDate(String inputString) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(inputString);

return date;
}

PGSQL date column :

@Temporal(TemporalType.DATE) @Column(name="patient_dob", nullable=false) private Date patientDob;
 

Wednesday, 27 January 2016

Java - Convert String containing decimal values to BigDecimal


Java Program to convert String containing decimal values into BigDecimal :
Sometime, we try to convert the String containing the decimal values into the Long. But since, int and long are Integer type so they can't have the decimal values to Long.valueOf(StringValue) gives the error.
The correct approach is to convert the string decimal into the BigDecimal or Double and use them as decimal values.

Example :
        String double_as_string = "23.23";
        BigDecimal price = new BigDecimal(double_as_string);

Tuesday, 26 January 2016

Java - Removing the trailing zeroes from a long number

Java Program to remove the trailing zeroes from the long digit :
Trailing zeroes can be removed from the long digit by using the DecimalFormat class. Below is one such example.

import java.text.DecimalFormat;
public class removeTrailingZero {
public static void main(String[] args) {
double answer = 5.0;
double answer1 = 4.50;
double answer2 = 5.25;
  DecimalFormat df = new DecimalFormat("###.##");
 System.out.println(df.format(answer));  
 System.out.println(df.format(answer1));
 System.out.println(df.format(answer2));
String x = df.format(answer);
System.out.println(x);
}
}

Monday, 11 January 2016

SQL - Adding a column in table from another table based on common column data in both table

We need to update values in table1 with data from table2 where common_column is the present in both tables and data should be updated based on this :

update table1 tl set table1_column=(select v.table2_column from table2 v where v.common_column= tl.common_column) where tl.table2_column is not null;  -- here condition can be any other condition 

Friday, 8 January 2016

Program to remove white spaces and special character from String in JAVA

JAVA Program to remove white spaces and special characters from String:

public class testRegular {
public static void main(String[] args) {
String text = "This - word ! has \\ /allot # of % special % characters ? this12340   _ ASGHJKK \t";
text = text.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(text);
}}


Here it will remove everything except alphabets [ small and capital ] and digits [ 0 - 9].

Similarly, we can make our permutation and combination of  arrangements in replaceAll method.


Tuesday, 22 December 2015

Java - Comparing two BigDecimal numbers

Comparing two BigDecimal Numbers :
java.math.BigDecimal.compareTo(BigDecimal val) compares the BigDecimal Object with the specified BigDecimal value.
JAVA PROGRAM :
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
// create 2 BigInteger objects
        BigInteger big1, big2;
        big1 = new BigInteger("6"); //first number
        big2 = new BigInteger("3"); // second number
        // create int object
        int res;
        // compare bi1 with bi2
res = big1.compareTo(big2);  
if( res == 0 )
System.out.println( "Both values are equal " );
else if( res == 1 )
System.out.println( "First Value is greater " );
else if( res == -1 )
System.out.println( "Second value is greater" );
System.out.println("comparing with zero");
if(big1.compareTo(BigInteger.ZERO)> 0)
{
System.out.println("bi1 is greater ");
}
       }
}