Monday, October 20, 2008
Oracle and null values
Employee ( id number, name varchar2(50) )
If i try inserting (1,'')into this above table guess what will be the name of the employee with id 1? Yeah its null. I thought that was weird, since i meant a 0-length string and not null. Alright, so i surrendered to the fact that Oracle would store a null every time I added a zero length string.
But that's not done. Now assume, my table has the following values
Id - Name
1 - null
2 - Anita
3 - null
4 - Beena
5 - Neha
What would this query return -
Select * from Employee where name not in('Anita', 'Neha');
If you thought it would return me 3 rows, you are wrong (as was I). it just returns one record -
4 Beena
Thats because when you fire a not in clause it will not consider null. So if you thought that null != Anita/Neha you are partially right but not completely
You have to explicitly change the above query as -
Select * from Employee where name not in('Anita', 'Neha') or name is null;
Morally of the story: Deal with null value columns carefully. A small mistake will be really difficult to debug
Wednesday, August 20, 2008
Modifying the right click in Win XP
1) To add the command prompt entry to your right click Open Explorer -> Tools(menu) -> Option -> FileTypes (tab) -> Folder Click on the Advanced button. In the dialog box , click on the New Button. In the new dialog box that pops up
Action: Command Prompt (any title)
Application used to perform action : cmd.exe
Click all the Ok buttons and your work is done. Now everytime you right click on any folder, you will see the Command Prompt option which you can use to directly go to the command prompt.
Note: Instead of choosing ‘Folder’(Open Explorer -> Tools(menu) -> Option -> FileTypes (tab)-> Folder) if you choose ‘File Folder’ you are in for some trouble. Everytime now you double click a folder, instead of opening it , it will open the command prompt. Now to solve this problem read point 2) below
2) To change the default action of a right click Many times after you add/remove new actions to a right click it may so happen that because of some mistake your default folder behaviour is changed. In my case every time I double clicked any folder it would open the command prompt (see note in point 1)). This is because some mistake of mine had affected the default behaviour of the folder double click. Set the HKEY_CLASSES_ROOT\Directory\shell to none and this problem should go away.
3) To remove the options added by various software’s when you install them. E.g. Winzip, Winmerge, etc You have to delete them from - HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers
Thursday, May 01, 2008
Regular Experssions and Java
Example 0)
String str = “someString”;
str.replaceAll(“*”,”#”);
The above snippet would replace all ‘*’ characters in the String str by ‘#’, simple, isn’t it? Well, if it’s so straight why is it here ;)
The replaceAll() API in String class has the following syntax – replaceAll(String regex, String replacement)
Yeah, I hope the term regex caught your attention. The first String is treated as a regular expression by Java and as a result the above snippet gives you a lovely error -
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
Lovely, because it uses the word dangling ;)
If you know regular expressions even a little the character ‘*’ has a special meaning – it means zero/more times. Let’s take some examples so that this is clear
Example 1)
String str = "the Blue Umbrella is bllue in collor";
System.out.println(str.replaceAll("ll*","?"));
Output: the B?ue Umbre?a is b?ue in co?or
Explanation: the regular expression ll* searches for strings {l, ll, lll,….} etc and the code replaces them with ?
Note: simple l* will search for {emptyString, l, ll, lll, ….}
Example 2)
String str = "the Blue Umbrella is blue in color";
System.out.println(str.replaceAll("rella.*","???"));
Output = the Blue Umb???
Explanation: ‘.’(dot) means any character, so .* would mean any character zero/more times. Hence everything from rella… gets replaced with ???
So in Example 0) to replace * with # you have to tell Java that do not take * in the regular expression term but as it is; so do a str.replaceAll(“\*”,”#”)….just escape your *….thats all!!!!
Wednesday, April 09, 2008
Java Object References
Someone was writing a parser and while unit testing he got bugged because the two lists that he was creating using the same objects was getting modified without he explicitly doing so. After having a look at the code it turned out to be a simple problem. In java, you deal with object references as against objects. So if you have
Object o1 = new Object(); Object o2 = o1; Object o3 = o1; You have a single object but 3 references. //heres a snippet of what my colleague was actually trying to do. He complained that list1 and list2 were getting modified on their own…Really??? Have a look
import java.util.ArrayList; class Emp { private int code; public Emp(int c) { code = c; } public void setCode(int c) { code = c; } public String toString() { return ""+code; }
}
public class References {
public static void main(String[] args)
{
ArrayList
p.s. Sorry about teh formatting....this stupid doesnt maintain the format that i write in and i am too lazy to use html tags and google docs is not working....so just pick the snippet and paste in a decent browser. it will work ;)