IT Certificaions

Google Cloud Platform

Wednesday, December 21, 2011

Get real client ip-address using java / getRemoteAddr() is giving 127.0.0.1 as client ip address in java


Getting real client ip-address using java (if Apache redirection is used...)
--------------------------------------------------------------------------
You can use ,
    String ipAddress=request.getHeader("X-Forwarded-For");
 if you have configured Apache redirection.
-------------------------------------------------------------------------

If you will use
     request.getRemoteAddr(), it may return 127.0.0.1 if apache redirection has been configured at your deployment server.



Tuesday, December 20, 2011

JDBC: Prepared Statement


Prepared Statement Demo using Java/MySQL
------------------------------------------------
package com.vicwalks.web.util;


import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import com.mysql.jdbc.Connection;


public class PreparedStatementDemo {
 public static void main(String[] args) {
 System.out.println("===========Prepared statement demo===========\n");
 Connection con = null;
 PreparedStatement preparedStatement;
 try{
   Class.forName("com.mysql.jdbc.Driver");
   con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASENAME","userName","password");
   try{
      String sql = "SELECT * FROM employee WHERE salary >= ?";
      preparedStatement = con.prepareStatement(sql);
      preparedStatement.setInt(1,10000);
      ResultSet rs1 = preparedStatement.executeQuery();
      System.out.println("List of employee where salary >= 10000");
      System.out.println("empId" + "\t- " + "empName"+ "\t- " +"salary");
      System.out.println("=============================================\n");
      while (rs1.next()){
        String empId = rs1.getString(1);
        String empName = rs1.getString(2);
        int salary = rs1.getInt(3);
        System.out.println(empId + "\t- " + empName+ "\t- " +salary);
      }
 
      System.out.println("=============================================\n");
      
      preparedStatement.setInt(1,15000);
      ResultSet result2 = preparedStatement.executeQuery();
      System.out.println("List of employee where salary >= 15000");
      System.out.println("empId" + "\t- " + "empName"+ "\t- " +"salary");
      System.out.println("=============================================\n");
      while (result2.next()){
        String empId = result2.getString(1);
        String empName = result2.getString(2);
        int salary = result2.getInt(3);
        System.out.println(empId + "\t- " + empName+ "\t- " +salary);
      }
 
      System.out.println("=============================================\n");
 
 
     }
     catch (SQLException s){
          System.out.println("SQL statement is not executed!");
     }
  }
  catch (Exception e){
       e.printStackTrace();
  }
}
 }


where userName: MySql user name, for example : root
          Password: MySql password


Output

 ===========Prepared statement demo===========


List of employee where salary >= 10000
empId - empName - salary
=============================================


001 - Anuj Kumar - 12000
003 - Amit Kumar - 11000
005 - Ajay Bisht - 18000
=============================================


List of employee where salary >= 15000
empId - empName - salary
=============================================


005 - Ajay Bisht - 18000
=============================================

Monday, December 19, 2011


Get <td> element value inside a <table>

1) Using JQuery.....

$('#tableDivId tr').each(function() {
    var tdText = $(this).find("td:first").html();    
});

Here you are iterating all rows of a table according to given div id, and then you are fetching first <td> element data for current <tr>....


2) Without JQuery...............

function loadAllTDs(){
var table = document.getElementById('tableDivId'); 
var rows = table.getElementsByTagName('tr');
var cells, tdText;

for (var i = 0, j = rows.length; i < j; ++i) {
      cells = rows[i].getElementsByTagName('td');
      if (!cells.length) {
         continue;
      }
      tdText = cells[0].innerHTML;  // For 0th td for each tr of this table....
      alert(tdText);    
    }
}

Tuesday, December 13, 2011

Executing FQL Queries :: FacebookResponseStatusException


com.restfb.exception.FacebookResponseStatusException: Received Facebook error response (code 601): Parser error: SELECT * is not supported.  Please manually list the columns you are interested in.
com.restfb.BaseFacebookClient$DefaultLegacyFacebookExceptionMapper.exceptionForTypeAndMessage(BaseFacebookClient.java:147)
com.restfb.BaseFacebookClient.throwLegacyFacebookResponseStatusExceptionIfNecessary(BaseFacebookClient.java:208)
com.restfb.DefaultFacebookClient.throwFacebookResponseStatusExceptionIfNecessary(DefaultFacebookClient.java:523)
com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookClient.java:485)
com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:445)
com.restfb.DefaultFacebookClient.executeQuery(DefaultFacebookClient.java:366)
com.vicwalks.web.util.SocialMediaUtil.findFacebookFriendsUsingRest(SocialMediaUtil.java:84)
com.vicwalks.web.action.FacebookConnectAction.execute(FacebookConnectAction.java:87)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)



Solution: You can use SELECT * FROM ... here to fetch User class data on the basis of id.

Please use the column name that you want to fetch, see User class:

id,  name,  about, bio, birthday, email, firstName, lastName, gender, location  etc........


String query = "SELECT uid, name FROM user WHERE uid=220439 or uid=7901103";
List<FqlUser> users = facebookClient.executeQuery(query, FqlUser.class);


System.out.println("Users: " + users);

...

// Be aware that FQL fields don't always map to Graph API Object fields.

public class FqlUser {
  @Facebook
  String uid;
  
  @Facebook
  String name;


  @Override
  public String toString() {
    return String.format("%s (%s)", name, uid);
  }
}

===> For more, visit the RESTFB API

get facebook friends using restfb

If you are using restfb (Facebook graph API and old REST API client wriiten in Java), and you want to access all friend list after Oauth authentication, you can use following method,



public List<ArrayList> findFacebookFriendsUsingRest(String facebookAccessToken){
  List<ArrayList> myFacebookFriendList= new ArrayList();
  final FacebookClient facebookClient;
  facebookClient = new DefaultFacebookClient(facebookAccessToken);
  User user = facebookClient.fetchObject("me", User.class);
  String userName = user.getFirstName();
  if (userName == null){
  userName = user.getLastName();
  }
  String userEmail = user.getEmail();
  com.restfb.Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
  System.out.println("Count of my friends: " + myFriends.getData().size());
  for(User friend: myFriends.getData()){
  System.out.println("Friends id and name: "+friend.getId()+" , "+friend.getName());
    myFacebookFriendList.add(friend.getName());
  }
  System.out.println("All Friends : "+myFacebookFriendList);
}




Wednesday, December 7, 2011

Android: MapActivity can not be resolved in android

For Eclipse users:

Please click on Project --> properties-->Resources-->Android--> Select Google api instead of Android

Now click on Ok.

Check again, Project--> properties--> Java build path, see the Libraries tab, expand Google APIs, and see if there is map.jar and android.jar are available or not. They must be.

Now clean your project and then run your project.


Tuesday, December 6, 2011

Android: where to run keytool command in android

Keytool command can be run at your dos command prompt, if JRE has been set in your  classpath variable.

For example, if you want to get the MD5 Fingerprint of the SDK Debug Certificate for android,

just run the following command...


C:\Documents and Settings\user\.android>  keytool -list -alias androiddebugkey  
                                                                -keystore debug.keystore -storepass android -keypass android


where C:\Documents and Settings\user\.android> is the path which consist the debug.keystore that has to be certified.





Android: MapActivity is not found

MapActivity is a special sub-class of  android.app.Activity. 


If you are using eclipse id, then click on AVD Manager and then it will show you the list of installed as well as un-installed packages. Select Google api packages and click on install. Then you will not this problem. if it is still set java build path


Now if this problem still occurs, see your build path... http://knowledge-serve.blogspot.com/2011/12/android-mapactivity-can-not-be-resolved.html