Find duplicate records in MySQL

Finding distinct records is easy in SQL by using DISTINCT keyword. But there is no such keyword to find duplicates. However, using following statement you find only those records that have duplicates of them in the table:

mysql> SELECT COUNT(id) as c FROM table_name GROUP BY id HAVING c > 1;

AJAX: Passing parameters to onreadystatechange function

When the response of an AJAX request is ready on the server the value of the readyState property of the XMLHttpRequest object is changed to “4″ which means the request is complete. The onreadystatechange property of the same object stores a user-defined anonymous function which is executed whenever the readyState is changed. A typical piece of code would be like:

xmlHttp.onreadystatechange=function () {
if(xmlHttp.readyState==4)
{
//do something with the response
}
};

However, sometimes it is required to pass one or more parameters to this anonymous function. It sounds tricky but it is pretty simple! This anonymous function can not take parameters but it can call another function defined in the same file and pass paramters to it. So, if you want to pass parameters to the anonymous function -you can do it as below:

xmlHttp.onreadystatechange=function () {
            stageChanged(parameter1, parameter2);
        };
    xmlHttp.open("GET", handlingURL, true);
    xmlHttp.send(null);
}

function stageChanged(p1, p2)
{
        if(xmlHttp.readyState==4)
        {
            //do something with the response
        }
}

Rapid website development

I am not seriously into PHP stuff. But it is a very good platform if you quickly want to come up with a dynamic website. It’s very easy and quick. Here are the tools that you could use:

1) XAMPP: It is a package of software that installs on your computer PHP, Apache HTTP Server and MySQL in a flash. From this package you can also choose to install FileZilla (an FTP client) and Mercury (a Mail Transport System). XAMPP will setup the whole server environment on your computer within minutes.

2) WordPress: WordPress is not just a blogging website. It is, in fact, a comprehensive platform for developing PHP based database driven websites. You can download WordPress and start building your website on it. Instead of creating “posts” (as bloggers do) you can create “pages” for your website. WordPress is highly customizable and loads of plug-ins are available to provide a variety of facilities on your webpages. By changing themes you can change the entire look-and-feel of your website in a jiffy.

3) MediaWiki: If you want to create a website where a community of users collaborate to develop the content -then MediaWiki is suitable for you (of course, there are other Wiki platforms available as well). MediaWiki is the platform that runs Wikipedia. You can download it and set it up (it doesn’t take more than 10 minutes) and your website is ready!… Just like WordPress, MediaWiki is also highly customizable and you can give your website a look that you like (meaning you are not bound to have a the Wikipedia-like interface)

Because both WordPress and MediaWiki are PHP based platforms. Though it is not necessary, if you have knowledge of PHP -it’s obviously even better.

Building strings in Java

Building and manipulating strings is one of the most common task performed in any programming language. In Java, the String class provides the functionality of creating and manipulating the strings (which are just sequence of characters):

String str="Lalit";

is equivalent to

char characters[]={'L', 'a', 'l', 'i', 't'};
String str=new String(characters);

The append operation on strings is often required while building a string. Generally, programmers use concatenation operator ( + ) to join two strings, like:

String str = "Java is ";
str=str + "cool";       // (or str+="cool";

But the instances of String class are immutable (i.e. unchangeable, read-only) -they cant be changed once created. So, to implement the string concatenation operator, Java uses the StringBuffer class (which is mutable).

String str = "Java is " + "cool ";

is internally implemented as:

String str = new StringBuffer().append("Java is ").append("cool").toString();

While building large strings it is better to use StringBuffer class and its methods (like append) instead of using String class and + operator. StringBuffer code will execute faster and will take lesser memory.

Scalable Vector Graphics and Inkscape

I am the founder administrator of an large Hindi language public wiki called Kavita Kosh. This Mediawiki based website is the largest Unicode resource of Hindi poetry on Internet. While working on this wiki in my spare time -I realized that Mediawiki discards the transparency from GIF and PNG images if they are used in a wiki website. So, I was wondering how Mediawiki based websites like Wikipedia manage to display icon images so nicely. I got the answer after a bit of research. Wikipedia icons are created as scalar vector graphics (SVG). Mediawiki keeps the transparency features of SVG files.

What are SVG images?

Scalar Vecor Graphics is a W3C recommendation based on XML. It specifies 2D vector graphics and also how to save them as a file. Cutting the jargon, SVG is a way of producing 2D graphics wherein the whole image is defined as XML tags. Behind every SVG file, there is XML… its just like every HTML page has a source code behind it.

Because SVG images are essentially XML text files -these images could searched, edited, indexed or compressed like any other text file. Also, unlike bitmap images, SVG images do not deteriorate when enlarged or shrunk. These images could be made animated as well. For more information about SVG you may refer to Wikipedia.

Though SVG could be created and edited using any text editor -there are tools available that help in this task. One such tool is Inkscape.

Inkscape

Inkscape is an open source software for SVG image editing. I found it amazingly good. It has most of the features that graphics editing tools should have. Personally, I very much liked it’s ability of drawing calligraphic text, 3D boxes, spirals, stars and polygons. The XML source is automatically generated in the background and you can manually edit it if you really want to.

Inkscape is a great tool. I would wish if it provides a facility to somehow save the created SVG images in formats such as  JPG, GIF and PNG as well.

What are Factory Methods and Why Use Them?

The concept of factory methods is very useful and very simple at the same time. It is simple enough to explain and I would not need to write a long post for that!

Basically a factory method is a method which returns an object (just like a factory manufactures goods -a factory method manufactures an object -and thats why the name). To understand it with an example, lets assume you have an interface, very creatively called MyInterface, which you want to implement. So, you implement it in a class called, say, FirstImplementation. And then you make use of FirstImplementation in, say, 120 odd other classes in your software. All ok so far!… But the problem will arise if you need to use a different implementation of MyInterface. Your options would be:

  1. Overwrite the code in the FirstImplementation with the code of new implementation
  2. Write the new implementation in another class, say, SecondImplementation and change all of the 120 odd classes to refer to the SecondImplementation.
  3. Use a factory class

Well, the first option is feasible. But in that case you will lose the code of the FirstImplementation. This is not a very neat way to solve the problem.

Second option is simply nightmarish! Only programmers as diligent as computers will attempt it as you will have to change 120 odd classes.

Third option is beautiful, neat and smart. You just create a new intermediate class, say FactoryClass, and use this class to get the instance of the FirstImplementation in your 120 odd classes. Now, if you would need to use a different implementation of MyInterface -all you need to do is just to modify the code in the FactoryClass.


public interface MyInterface{

//define members}

public class FirstImplementation{

//implement MyInterface}

public class FactoryClass{

//constructor come here

//write more methods if needed

public MyInterface getInstance(){

MyInterface a=new FirstImplementation();

return a;

}

public class UserClass{

FactoryClass faObj = new FactoryClass();

MyInterface myInter=faObj.getInstance();

}

 

So now if you decide to implement MyInterface as SecondImplementation -all you need to do is to change the code in the FactoryClass. You don’t need to touch the UserClass(es) for this.

MySQL: Taking backup by dumping

Sometimes we need to take backup of a single tables from a database (for example, when we want to transfer data from one server to another. This is easily achieved by using the mysqldump command:

shell > mysqldump -ustar -purpwd db_name table_name > file_name.sql

-u and -p options specify the user name and password. There should not be a any space between -u & user name and -p & password. It is not a must to keep the extension of file as .sql but because this file would contain the SQL statements -so keeping .sql makes more sense

This dump will have SQL command for dropping the table if it already exist in the target database.

In order to import the dumped table into a database, use a command as below:

shell > mysql -ustar -purpwd db_name < y1.sql

Place two DIVs side by side

We often require to put two (or more) DIV elements side by side. Those who are not experienced in CSS tricks spend quite a bit of time in doing so and in the end they realize that the job was, in fact, pretty easy. If you are want place two DIV elements side by side, do the following:

<DIV id="container">
<DIV style="float:left;margin-top:10px;background-color:#ff0000;width:40px;height:40px"></DIV>
<DIV style="float:right;margin-top:10px;background-color:#0000ff;width:40px;height:40px"></DIV>
</DIV>

The float and margin tags do the trick. If you wont use margin, the DIVs will be placed by the browser on top of the each other.

Java and memory

I wrote a Java application which, when run, requires quite a bit of memory and I started to get the OutOfMemory error:

Error: java.lang.OutOfMemoryError: Java heap space

This often happens when we need to run memory-guzzling programs. Following are a few ways to resolve such problems.

What is heap space?

First of all, lets understand what is heap space. Well, Java uses memory mainly in the arrangements of heap and stack. Java keeps the parameters, that are passed from a calling method to a called method, in the form of a stack structure (i.e. parameters are pushed in and popped out of the stack as per the need). On the other hand, heap is a randomly accessed memory space where Java creates and keeps objects. Java creates the object within this heap space wherever it finds room enough to contain the object. So, when you create a new object (e.g. new String(); ) the object gets created in the heap space and occupies the memory which it can fit into. During the garbage collection, Java frees the heap space of those objects which are no longer referenced. But if the number of referenced (i.e. “required”) objects keeps on increasing and we don’t dereference them -the JVM runs out of the heap space.

The amount of memory that a class would require depends upon the number and type of its data members.

  1. The class itself requires minimum of 8 bytes
  2. Every data member requires 4 bytes (long and double require 8 bytes). Even a boolean variable is use 4 bytes though it only needs one bit to get stored.
  3. Amount of used memory grows in chunks of 8 bytes

Optimize your program

The first step to get around this problem should be to go through the program and see how it could be modified so as to make it use lesser memory. Since long the cost of memory has been extremely low in comparison with what it was during 1960s. And programmers have become pretty extravagant in terms of memory allowance to their programs. However still, more often than not, we need to write programs that require more memory than available. Frugality in context of memory usage is always good because not only it reduces the chances of program crash but also sometimes it enhances the speed of execution.

You should set objects to be null when they are no longer required in your code. This is even more important if you are running a loop in which you create object(s) but then don’t discard them when they are not required.

Make use of garbage collector method

The second step towards the memory usage optimization could be to explicitly call the Garbage Collection method. Java automatically calls this method as and when required and frees up the memory space that contains the objects which are no longer referenced. But you can also call this method explicitly in your code (for example, after a “big task” is accomplished and next “big task” is about to begin). To call the garbage collector, use:

Runtime.getRuntime().gc();

You can monitor the status of memory before and after gc() method is called.

System.out.println("\n\nCalling Garbage Collector\n\n");
System.out.println("Free memory BEFORE: " + Runtime.getRuntime().freeMemory());
Runtime.getRuntime().gc();
System.out.println("Free memory AFTER : " + Runtime.getRuntime().freeMemory());
System.out.println("\n\nGarbage collection finished\n\n");

freememory() method returns amount of free memory available measured in bytes. For other methods provided by Runtime class see Java API

Give more memory to JVM

Efforts for memory usage optimization notwithstanding, sometimes JVM does indeed needs more memory. In such cases we can tell JVM via command line to use a specified amount of memory:

java -Xms2000m -Xmx3500m <Class name>

-Xms and -Xmx switches specify the amount of swap space (virtual memory) that JVM can use in your machine.

-Xms specifies the minimum (i.e. initial) amount of memory

-Xmx specifies the maximum amount of memory that JVM could use.

These memory values should be specified in Megabytes.

Names and IDs of Elements

An element in a web page could be represented either by a name or by an id or by both. For example:

<div name="divName" id="divID">
div content
</div>

If the element is part of a FORM -the name of the element is sent to server along with the value of the element when the form is submitted. Therefore name is important for the name-value pairing in the query string that goes to server.

The ID of the element is generally used to refer to the element in client-side scripts (like JavaScript). This is accomplished by getElementById() method of the document object in the DOM model. For example:

var foo = document.getElementById('elementID').value;

When we assign names and IDs to the elements -it is quite tempting to keep them same (especially in large applications where either we run out the logical/sensible names or find it difficult to keep track of them), like:

<div name="foobar" id="foobar"></div>

This practice is mostly harmless -but I have noticed that it sometimes causes problems in context of AJAX (especially in IE). A few of my AJAX scripts which worked fine in FF; did not work in IE and Safari. Eventually the problems were solved when I made the names and IDs of the used elements different. I think this problem is application specific and will not appear in every AJAX script. However, I guess it is a good practice to keep the names and IDs different. To get around the issue of running out of the sensible names -I just suffix “ID” to the id of the element while keeping rest of the string same as that of it’s name. e.g.

<div name="foobar" id="foobarID"></div>