Tuesday, May 17, 2011

How to disable enable user input in queries


Many times it might have been required to insert & symbol in the value of a column in SQL.

How do you guys pass that.

There might be more than one ways to do that

Here is one of them.

set scan off;

This will disable SQL to ask for user input if any subsequent query contains & symbol.

You can ofcourse use set scan on; again to enable it back.

Wednesday, April 27, 2011

Problem in session Handling


Many of you might have faced this problem earlier as well. But this is a very new and recent learning which I have gained during my work at Nucleus Software. 



Many times I have seen, that where i expect the web-application to pick up and already existing session, it is creating a new session, when a request.getSession() is encountered.
I was unable to understand the reason, why? Then one colleague of mine helped me solve this problem. 
Still I am not 100% sure of the root cause of the problem, but this trick has solved my problem



Advanced Privacy Settings

You can override automatic cookie handling for all Web sites in the Internet zone by clickingAdvanced on the Privacy tab. You can use the Advanced Privacy Settings dialog box to configure first-party and third-party cookies to Accept, Block, or Prompt, with a check box to always allow session cookies. 





After doing this, session.isNew() started returning false and the problem was solved. 

Tuesday, April 19, 2011

Sunday, April 10, 2011

to_char and to_date function in oracle

Source : http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html

When a DATE value is displayed, Oracle must first convert that value from the special internal format to a printable string. The conversion is done by a function TO_CHAR, according to a DATE format. Oracle's default format for DATE is "DD-MON-YY". Therefore, when you issue the query
select b from x;

you will see something like:
B
---------
01-APR-98


Whenever a DATE value is displayed, Oracle will call TO_CHAR automatically with the default DATE format. However, you may override the default behavior by calling TO_CHAR explicitly with your own DATE format. For example,
SELECT TO_CHAR(b, 'YYYY/MM/DD') AS b
FROM x;


returns the result:
B
---------------------------------------------------------------------------
1998/04/01
The general usage of TO_CHAR is:
TO_CHAR(, '')


where the  string can be formed from over 40 options. Some of the more popular ones include:
  , for example.

MMNumeric month (e.g., 07)
MONAbbreviated month name (e.g., JUL)
MONTHFull month name (e.g., JULY)
DDDay of month (e.g., 24)
DYAbbreviated name of day (e.g., FRI)
YYYY4-digit year (e.g., 1998)
YYLast 2 digits of the year (e.g., 98)
RRLike YY, but the two digits are ``rounded'' to a year in the range 1950 to 2049. Thus, 06 is considered 2006 instead of 1906
AM (or PM)Meridian indicator
HHHour of day (1-12)
HH24Hour of day (0-23)
MIMinute (0-59)
SSSecond (0-59)

Friday, March 11, 2011

Tips in Working with Firebug

Many thanks to http://www.ajaxwith.com/Tips-in-Working-with-Firebug.html for sharing the knowledge with us.

Tips in Working with Firebug


Firebug is considered one of the essential tools for every developer whether they are working with JavaScript or not. It’s a simple add-on for Firefox but it can provide essential feedback for the application not normally noticed by the naked eye. It pinpoints small errors in your site which might not really change the looks of your online application but it could change the way it behaves. Simply put, Firebug could help your application improve by noticing the small things. Optimization has never been simpler with Firebug. Plus it’s free.

Wednesday, March 2, 2011

Tips and Tricks in javascript

Source : http://www.ajaxwith.com/Use-Arguments-Define-Functions-JavaScript.html

Use Arguments to Define Functions in JavaScript


Functions are extended or limited in an Ajax and JavaScript based applications through parameters. With the aid of this small tool, developers can customize their functions so that it could properly interpret different types of information and/or commands. It is also very straightforward which also aids in client side interpretation.

But parameters are not a requirement in an Ajax and JavaScript functions. Instead of using parameters, it is also possible for developers to use arguments. This is a small trick in JavaScript and Ajax which could be used in certain situations which ultimately eases web development. It is a bit tricky but works without any problem in any browser. You do not have to push any definitions for your parameters – you can allow arguments to do the same task.

Understanding Parameters

Before continuing with the arguments, first understand the role of parameters. Parameters are practically a “limiter” or a tool that provides a different interpretation. A definition is a good addition to parameters so that it can be properly identified. The identified is implemented within () so that it can be invoked when the parameter is used.

Instead of using a definition within the parenthesis of a parameter, you can use an argument. The use of argument in JavaScript is not unique compared to other programming language. However, the idea of “array is not an array” could be attributed to the argument. Here is an example:

function enterName(a,b,c,d){
     console.log(arguments)
}

The arguments added in this example help define the functions entered. This is where you can see the other use of arguments – arguments define the data entered in the function which is practically the same role required in the parameter.

When you combine a parameter and an argument, you will get something like this:

function(//variable with parameters// //argument function//; operation)

It is a straightforward method of adding argument or defining the parameters. Notice in the end that you are adding an operation. The argument will require an operation which interprets the data entered in the function.

Small Downsides

Although this feature on arguments is quite impressive, it has its shortcomings which should be considered by any developer:

• Not a JavaScript array – As already indicated, arguments are perfect examples of an “array is not an array”. This type of implementation is not a JavaScript array. When implemented in this manner, the argument becomes another object. This means it cannot use other functions related to arrays.

• Referencing not Interpreting – This can easily slow down an application. Although this form of coding is accepted in major browsers, the arguments will not be considered essential. This means the arguments will only become a reference and this could slow down the rest of the functions attached to the arguments.

If you are planning to build a small Ajax and JavaScript application, this trick can work to your advantage. Simply add the arguments to the variables so that the parameters can be read with the arguments. The only downside is that the arguments become objects and it could slow down data intensive transactions. It is a simple trick that can be useful in some online applications.

Tuesday, March 1, 2011

How to disable USB on your computers

1. Start -> Run -> regedit
2. Go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR
3. Click on start in right pane.
4. Give value as 4. (to block USB)
5. Give value as 3. (to unblock USB)

This is not a tested solution, but we found this somewhere on internet.

Monday, February 21, 2011

Interview questions -- Serializable

Suppose I have 2 classes
1) Person having person name = "Yogesh";
2) User having username = "User" extends Person and implements Serializable.

What will be the values printed if I serialize and deserialize the User object?

I guessed it wrongly and answered that personname will not get printed as Person class is not serializable. BUT THAT WAS WRONG !!!

Both the values will be stored and retrieved correctly. The reason I don't know, but, this scenario, I have tested on PC

Saturday, February 19, 2011

What is serialVersionUID?

Source : http://www.javablogging.com/what-is-serialversionuid/

What is serialVersionUID?


Most people learn about serialVersionUID after they write their first serializable object (I know I did). You add ‘implements Serializable’ and in the next moment your IDE starts complaining… so what’s up?

Lets look at a simple example to see what meaning that variable has. In the example we will use the class SerializeMe shown below:

class SerializeMe implements Serializable {
    private static final long serialVersionUID = 1L;

    private int data;

    public SerializeMe (int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }
}

Field data represents some information stored in the class. Class implements the Serializable interface, so my IDE automatically offered me to declare the serialVersionUID field. Lets start with value 1 set there.

We will also need some class that will serialize and deserialize the SerializeMe class. Here it is:

public class UIDTester {
    public static void main(String... strings) throws Exception {
        File file = new File("out.ser");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        SerializeMe serializeMe = new SerializeMe(1);
        oos.writeObject(serializeMe);
        oos.close();

        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);

        SerializeMe dto = (SerializeMe) ois.readObject();
        System.out.println("data : " + dto.getData());
        ois.close();
    }
}

This code will serialize an instance of class SerializeMe to a file and then deserialize it back. Now let’s run the main function! You should get output that says:


data : 1

Tuesday, February 15, 2011

Best practices in Collections

If you do need a synchronized map, you can use the static method synchronizedMap(...) in class Collections to add synchronization to a regular map. Don't use the legacy collection classes, such as Hashtable. Example:

// A regular, unsynchronized map  
Map<Integer, String> map = new HashMap<Integer, String>();  
  
// Wrap it to add synchronization  
Map<Integer, String> synchedMap = Collections.synchronizedMap(map); 

Common Mistakes in Collections

Source : http://javabeanz.wordpress.com/2007/07/13/treemap-vs-hashmap/

Classes like Integer, String, Double etc implements Comparable interface. So if we are to use an object of a custom class as the key, ensure that it’ s class implements the Comparable interface.

public class MyCustomKey implements Comparable
{
    private int value;
    public MyCustomKey(int value)
    {
       this.value = value;
    }           

    public int compareTo (MyCustomKey key)
    {
       int comparison = 0;           

       // Note:
       // Return -1 if this.value < key.value
       // Return  0 if this.value = key.value
       // Return  1 if this.value > key.value           

       return (comparison);
    }
}


A common mistake that everyone does is not to override the hashcode(). If we are failing to do so, map.get(new MyCustomKey()); may not give you what you were expecting. So it is always advised to override the hashCode() if objects of that class is being used as a key.

public class MyCustomKey implements Comparable
{
    private int value;
    public MyCustomKey(int value)
    {}
    public int compareTo (MyCustomKey key)
    {}        

    public int hashCode()
    {
        // Note:
        // If two objects are equal then their corresponding hashCode ()
        // should return the same value too.
        return (this.value * 199);
    }
}


When you are using your custom object as a key to a HashMap, make sure you do the following

1) implement Comparable
2) override compareTo method, and give its implementation
3) override hashCode and equals method, and give their implementation.
4) Always, make your key object as immutable, so that it is not changed after you add it to a HashMap as key.

Sunday, February 13, 2011

Struts2 vs. Struts1


1.  Servlet Dependency:
Actions in Struts1 have dependencies on the servlet API since theHttpServletRequest and HttpServletResponse objects are passed to the execute method when an Action is invoked but in case of Struts 2, Actions are not container dependent because they are made simple POJOs. In struts 2, the servlet contexts are represented as simple Maps which allows actions to be tested in isolation. Struts 2 Actions can access the original request and response, if required. However, other architectural elements reduce or eliminate the need to access the HttpServetRequest or HttpServletResponse directly

Struts 2 Iterator tag example

Source : http://www.mkyong.com/struts2/struts-2-iterator-tag-example/

Struts 2 Iterator tag example


Struts 2 Iterator tag is used to iterate over a value, which can be any of java.util.Collection or java.util.Iterator. In this tutorials, you will create a list variable, use Iterator tag to loop over it and get the iterator status with IteratorStatus.

1. Action
An Action class with a List property , which contains variety of delicious “KFC combo meals”.

IteratorKFCAction
package com.mkyong.common.action;
 
import java.util.ArrayList;
import java.util.List;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class IteratorKFCAction extends ActionSupport{
 
 private List<String> comboMeals;
 
 public Listlt;String> getComboMeals() {
  return comboMeals;
 }
 
 public void setComboMeals(List<String> comboMeals) {
  this.comboMeals = comboMeals;
 }
 
 public String execute() {
 
  comboMeals = new ArrayList<String>();
  comboMeals.add("Snack Plate");
  comboMeals.add("Dinner Plate");
  comboMeals.add("Colonel Chicken Rice Combo");
  comboMeals.add("Colonel Burger");
  comboMeals.add("O.R. Fillet Burger");
  comboMeals.add("Zinger Burger");
 
  return SUCCESS;
 }
}

Friday, February 4, 2011

Column names in hbm.xml

When i added a field in my pojo class, with name xMoveCompatibleItem and I used eclipse's inbuilt feature to generate getter and setter methods

Eclipse generated methods with the following names:
getXMoveCompatible()
setXMoveCompatible(String)

Note that X and M are both capital and consecutive, which was creating a problem for weblogic. It was not able to find the getter method for that property.

Do take care while adding a property, if the second letter of the property name is in CAPITAL