Monday, 28 February 2011

Installing MySQL on Solaris (Part 3 - Running the mysql Demon)

Running MySQL as a Demon
This small blog demonstrates the steps required to start-up MySQL as a demon at boot time. The first step is to copy the mysql file from the installation directory (/opt/mysql/mysql/bin)
to the /etc/init.d directory and ensure that it’s executable (chmod 755 mysql).

Sunday, 27 February 2011

Installing MySQL on Solaris (Part 2 - Changing Passwords)

Change the Passwords

Don’t forget to change the passwords for the root and unknown users:

Saturday, 26 February 2011

Installing MySQL on Solaris (Part 1 - Installing the files)

To install MySQL on Solaris, you first need to download it from the MySQL Community Server website.

Before installing MySql, the first job is to create the mysql group by typing: 

groupadd mysql

Next, create the mysql user by typing: 

useradd -g mysql mysql

Friday, 25 February 2011

Modulo and Negative Numbers

Did you know that the modulo of a negative number is negative? In the presumed words of Micheal Caine it seems that “not a lot of people know that”. For example its obvious that:

5 % 2 = 1

but it isn’t so obvious that:

Thursday, 24 February 2011

An Easy Singleton

Writing a Singleton class can be a bit of a chore, you have to remember to do several things such as making your constructor private, implementing a proper getInstance() factory method and making it thread safe by making your instance variable volatile.

There is a quicker way, however, you can cheat and implement a singleton using an Enum:

Wednesday, 23 February 2011

When Object Design Goes Wrong...

There are a good many well documented 'code smells' and 'anti-patterns', but I've always found the following couple of items useful...

Tuesday, 22 February 2011

OO Revision - a blast from the past

I recently came across this object revision list, taken from Booch's Object Oriented Analysis and Design which I bought in 1995 and referencing OMT. It was written using my previous first language of C++, something I've not used in a good while. It was good to read through it after all these years, giving the critic of time, just to figure out what's changed over the years.

Monday, 21 February 2011

The Leap Year Rule

This is the algorithm to use when working out if a year is a leap-year or not...

Sunday, 20 February 2011

Remote Access to MySQL databases

One way in which MySQL differs from other databases such as Oracle, is that by default, only accounts on the same machine as the database are allowed to access the database. This means that, although it’s not heavily documented, all user names consist of two parts: the user and the machine name (or IP address); hence on the local machine, a user called ‘fred’ is really, by default, called ‘fred@localhost’ - although the ‘localhost’ part is usually hidden.

Saturday, 19 February 2011

Caffeine Power

Caffeine - the programmers drug (well at least one of them). You many have noticed that the majority of programming groups use caffeine heavily, but why is this? Well it keeps you awake and allows you to do your often boring job (not all programming tasks are interesting), it speeds up your thoughts and pumps blood more quickly round your body.

Friday, 18 February 2011

Locus of Control

As a software manager, one of your main aims is to have well-motivated staff. There are many theories about motivation from Maslow’s Needs to ones that involve lots of “ra-ra” shouting.

Thursday, 17 February 2011

Exceptions and Overriding

When you extend a class and override a method, the Java compiler insists that all exception classes thrown by your overridden method must be the same as, or subclasses of, the exception classes thrown by the original method in the base class. The code below demonstrates a base class being overridden by various subclasses.

Wednesday, 16 February 2011

Maven and Eclipse Integration (m2eclipse)

A good way to do eclipse / Maven integration is to use m2eclipse from Sonatype. To install this eclipse plugin, go to Help | Install New Software, when the dialogue will appear. Then click ‘Add...’ button and add the following URL: together with some description or other. Finally, click Next and follow the instructions...

Tuesday, 15 February 2011

Eclipse and Subversion Integration

A good way to do eclipse / Subversion integration is to use Collabnet’s Desktop for eclipse. To install this plug-in go to Help | Install New Software, when the dialogue will appear. Then click Add...’ button and then add the following URL: together with some description or other... finally press Next and continue as directed.

Sunday, 13 February 2011

Adding Log4j to a Weblogic Domain Configuration

One of the things I often have to do is install Weblogic Server for development purposes, which often means making manual changes to an installation created by the install wizard and the way to do this is to modify the setDomainEnv file. There are two copies of this for every Weblogic installation: one for Windows, and the other for Unix. Given that I generally do most development under Windows, then I’ll be using setDomainEnv.cmd in this example, which demonstrates how to add log4j to the installation.

Saturday, 12 February 2011

Implementing the equals() hashCode() Contract

The Java documentation for the Object.equals() method states that: "it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes." This is rather vague, is it necessary, or just a good idea to implement hashCode() when you override equals()?

Friday, 11 February 2011

String and the StringBuilder Usage

One common mistake you often see is code that relies on the repeated concatenation of strings using the String class. String concatenation using the String class okay when all you need to do is concatenate one string to another, but when it comes down to concatenating multiple strings, this method generates serious time penalties.

Thursday, 10 February 2011

Arithmetic Precedence

The order for arithmetic precedence is:

* / % + -

Wednesday, 9 February 2011

Floating Point Numbers

Floating point numbers have the following range:
  • Negative Values
  • -0.0
  • 0.0
  • Positive Numbers

Tuesday, 8 February 2011

What do all the different HashMaps do?

It seems strange to me that the Java API has been allowed to grow to the point where there’s so much overlapping functionality. If, as a Java API developer, you were allowed to ‘refactor mercilessly’ then there would be whole swathes of classes that would suddenly find themselves deprecated.

Monday, 7 February 2011

Using java.util.Collections - 2

Yesterday’s blog, contained a quick example of using Collections.reverse(), which, from experience, is something that you don’t have to do that often; so today’s post demonstrates a more useful example. Now, one thing you often need to do is to write a method that returns a Collection (eg a List, Set, Map etc.) to its caller.

Sunday, 6 February 2011

Using java.util.Collections - 1

One of the most useful classes is the java.util.Collections consisting of static methods that operate on or return collections. So if you need to reverse or sort a list, for example, then don't write the code yourself just look here!

Saturday, 5 February 2011

Simplify your Beans

One of the boring grunty tasks that you always seems to end up having to do is to implement a toString() method on your plain old Java beans, data transfer objects or value objects. That’s because at some point during your development cycle you’ll need to know the values returned by each of your ‘getter’ methods in order to visually verify its state.

Friday, 4 February 2011

The Serializable and Externalizable Iterfaces

The difference between Serializable and Externalizable interfaces and when to use each one...
public interface Externalizable extends Serializable
Externalization is similar to Serialize except that an object is responsible for saving its own state using methods calls:

Thursday, 3 February 2011

Serializing an Object

1) Serializable does not define any methods. It tells the JVM that a class can be serialized.
2) If your class extends a class that doesn't implement Serializable then that super class must have a default (no argument) constructor to initialise the itself during de-serialization.
3) If you try to serialize a class that doesn't implement Serializable then a NotSerializableException run time exception is thrown.
4) Serialization is done through a classes that implement the ObjectOutputStream and the corresponding ObjectInputStream interface.

Wednesday, 2 February 2011

Use the WeakHashMap

A WeakHashMap is a hashmap implementation that uses weakly reachable keys. What this boils down to is this... when an object is added to a normal hash map then the key is strongly referenced by the thread that added it to
the map. In the case of a WeakHashMap a WeakReference class is used to wrap the key to the object. The WeakHashMap then uses it's own ReferenceQueue so that it is notified of keys that have been garbage collected. Once there are no other references to the key then the object will be removed from the WeakHashMap automatically. The big idea here is to stop problems with memory leaks that can occur in hash maps if the programmer forgets to remove an object from the map once the program has finished with it.

Tuesday, 1 February 2011

Method arguments are always passed by value

This includes object references which also passed by value and copied. However in this case the object itself is not copied and you end up with two references to the same object.

Consider the output from the code below: