Code snippets

This blog would be providing you one stop solution for the clean solutions towards solving the problem, instead of just giving just instruction and not actual code.

Featured Posts

15 Feb 2014

Developer options Android 4.3

No comments :
You can't find developer options in Android 4.3. It has been hidden very playfully. Here's the trick to enable it: settings-->About Phone-->tap "Build number" 7 times step by step guide is coming soon. with images.

20 Nov 2013

How to take backup of a table in oracle?

No comments :
CREATE TABLE EMPLOYEE_BACKUP AS SELECT * FROM EMPLOYEE;

15 Oct 2013

JAXB Issues in JCAPS

No comments :

There are few issues  you might face while using JAXB API in JCAPS (Java CAPS).

1. JAXB import statements throw errors, even though Jars are available in common library path.
Somehow JCAPS does not pick up these jars. Do no waste your time and import these libraries in your project itself.


2. JABXContext.createContext(Class) throws error.
Not sure why JCAPS throws error even though the method is available in jaxb api jar.
In this case, use below solution

// Provide comma separated list of packages
JAXBContext jaxbContext = JAXBContext.newInstance("com.test.messaging:com.package2");

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

MessageType message =  (MessageType )jaxbUnmarshaller.unmarshal(file);


If you are facing ClassCastException  on third line, you might want to use this solution.JAXB Unmarshal Classcast Exception


JAXB unmarshal returns JAXBElement instead of root element

No comments :

If you are facing ClassCastException while unmarshaling XML using JAXB, it might be happening because unmarshalling is returning JAXBElement object.

Use below solution if JAXB unmarshaling returns JAXBElement.


JAXBContext jaxbContext = JAXBContext.newInstance("com.test.messaging");
 
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

JAXBElement jaxbResponse  =  (JAXBElement)jaxbUnmarshaller.unmarshal(file);

MessageType message = (MessageType )jaxbResponse.getValue();

13 Oct 2013

CSS templates for various standard screen sizes with media queries

No comments :
No talk, just the code.

/* Iphone (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* CSS style */
}

/* Smartphones - landscape ----------- */
@media only screen 
and (min-width : 321px) {
/* CSS style */
}

/* Smartphones -portrait ----------- */
@media only screen 
and (max-width : 320px) {
/* CSS style */
}

/* iPads - portrait + landscape ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* CSS style */
}

/* iPads - landscape ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* CSS style */
}

/* iPads - portrait ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* CSS style */
}

/* Desktops + laptops ----------- */
@media only screen 
and (min-width : 1224px) {
/* CSS style */
}

/* Large displays ----------- */
@media only screen 
and (min-width : 1824px) {
/* CSS style */
}

/* iOS 4/4s */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* CSS style */
}

Plain JDBC connection to Oracle DB

No comments :
No talk, just the code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author CK
 */
public class TestJDBCwithOracle {
 public static void main(String[] args) throws Exception {
  Class.forName("oracle.jdbc.driver.OracleDriver");
  Connection conn = DriverManager.getConnection("jdbc:oracle:10.128.69.124:1521:USDBTOT", "scott", "tiger");

  Statement stmt = conn.createStatement();
  ResultSet rset = stmt
    .executeQuery("select sysdate from dual");
  while (rset.next())
   System.out.println(rset.getString(1));
  rset.close();
  stmt.close();
  conn.close();
 }
}


How to convert Collection to Array?

No comments :
No talk, just the code.

package poc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author CK
 *
 */
public class CollectionToArray {

 /**
  * @param args
  */
 public static void main(String[] args) {
  List arraylist = new ArrayList();
  arraylist.add("Apple");
  arraylist.add("Orange");
  arraylist.add("Grape");
  arraylist.add("Pear");
  Object[] array = arraylist.toArray();
  System.out.println("\nThe objects into array.");
  for(Object name: array)
  {
   System.out.println(name);
  }
 }
}


How to insert data in Mongolab programatically in Java?

No comments :
Assumptions
  • Mongolab database is already created
  • Audience is aware of Java programming
Step-1: Get mongolab databse API key
1) Login to mongolab at below link.
https://mongolab.com
2) Click on username link on top right corner of the page.
3) Copy the URL and API key from the bottom of the page.
"https://api.mongolab.com/api/1/databases/{{database name}}/collections/{{collection name}}?apiKey={{API key}}"
Step-2: Java program to use the API key
    package poc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class InsertJSON {

 private final String USER_AGENT = "Mozilla/5.0";
 private static final String MONGOLAB_DATABASE = "MONGOLAB_DATABASE";
 private static final String MONGOLAB_COLLECTION = "MONGOLAB_COLLECTION";
 private static final String MONGOLAB_API_KEY = "b_6OqxduIanXrxocmvTsAXubns5iXYZ";
 private static final String _URL = "https://api.mongolab.com/api/1/databases/"+MONGOLAB_DATABASE+"/collections/"+MONGOLAB_COLLECTION+"?apiKey="+MONGOLAB_API_KEY;
 public static void main(String[] args) {

  try {
   for(int i=100;i<=200;i++){
    URL url = new URL(_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");

    String input = "{"+
      "\"_id\": \""+i+"\",    \"episode\": \"22\",    \"date\": \"12-Aug-2012\",    \"name\": \"Hitlardidi\",    \"channel\": \"Zee\",    \"keyword\": \"test\","+
      "\"poster\": \"http://funrulz.com/wp-content/uploads/2013/06/Hitler-Didi.jpg\",    \"site\": \"https://www.youtube.com/watch?v=xtIRohx67i8\"}";

    OutputStream os = conn.getOutputStream();
    os.write(input.getBytes());
    os.flush();
    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();

  }
 }
}

12 Oct 2013

Java mail changes for the best view.

No comments :
The following code block is what I would like to syntax highlight:
    // Define our Function
    function checkMeaningOfLife ( decimal, success ) {
        if ( parseInt(decimal,10) === 42 ) {
            window.console.log(success);
        }
    };
    // Define our Variables
    var str = 'The meaning of life is true',
        decimal = 42.00;
    // Fire our Function
    checkMeaningOfLife(decimal,success);

9 Oct 2013

Java regular expression

No comments :
Regular expressions